To fully understand the difference between get_categories()
, get_category()
and get_term_by()
, you need to have a look at how these functions are constructed.
get_categories($args)
as its name suggest, get a list of all categories created on the site. This function can be found in “wp-includes/category.php” lines 39 to 66
39 function get_categories( $args = '' ) {
40 $defaults = array( 'taxonomy' => 'category' );
41 $args = wp_parse_args( $args, $defaults );
42
43 $taxonomy = $args['taxonomy'];
44 /**
45 * Filter the taxonomy used to retrieve terms when calling get_categories().
46 *
47 * @since 2.7.0
48 *
49 * @param string $taxonomy Taxonomy to retrieve terms from.
50 * @param array $args An array of arguments. @see get_terms()
51 */
52 $taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args );
53
54 // Back compat
55 if ( isset($args['type']) && 'link' == $args['type'] ) {
56 _deprecated_argument( __FUNCTION__, '3.0', '' );
57 $taxonomy = $args['taxonomy'] = 'link_category';
58 }
59
60 $categories = (array) get_terms( $taxonomy, $args );
61
62 foreach ( array_keys( $categories ) as $k )
63 _make_cat_compat( $categories[$k] );
64
65 return $categories;
66 }
As you can see from line 40, category is used as the default taxonomy, so only categories will be returned in the list of categories
get_category( $category, $output, $filter )
only retrieves the category data for a specific given category. This function can be found in “wp-includes/category.php” lines 90 to 99
90 function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
91 $category = get_term( $category, 'category', $output, $filter );
92
93 if ( is_wp_error( $category ) )
94 return $category;
95
96 _make_cat_compat( $category );
97
98 return $category;
99 }
From line 91, you can see that get_category()
only uses category as the term, so only category will be returned, no terms.
get_term_by( $field, $value, $taxonomy, $output, $filter )
will get all term data by term field and data. This function can be found “wp-includes/taxonomy.php”in lines 1028 to 1076
1028 function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
1029 global $wpdb;
1030
1031 if ( ! taxonomy_exists($taxonomy) )
1032 return false;
1033
1034 if ( 'slug' == $field ) {
1035 $field = 't.slug';
1036 $value = sanitize_title($value);
1037 if ( empty($value) )
1038 return false;
1039 } else if ( 'name' == $field ) {
1040 // Assume already escaped
1041 $value = wp_unslash($value);
1042 $field = 't.name';
1043 } else if ( 'term_taxonomy_id' == $field ) {
1044 $value = (int) $value;
1045 $field = 'tt.term_taxonomy_id';
1046 } else {
1047 $term = get_term( (int) $value, $taxonomy, $output, $filter);
1048 if ( is_wp_error( $term ) )
1049 $term = false;
1050 return $term;
1051 }
1052
1053 $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value) );
1054 if ( !$term )
1055 return false;
1056
1057 wp_cache_add($term->term_id, $term, $taxonomy);
1058
1059 /** This filter is documented in wp-includes/taxonomy.php */
1060 $term = apply_filters( 'get_term', $term, $taxonomy );
1061
1062 /** This filter is documented in wp-includes/taxonomy.php */
1063 $term = apply_filters( "get_$taxonomy", $term, $taxonomy );
1064
1065 $term = sanitize_term($term, $taxonomy, $filter);
1066
1067 if ( $output == OBJECT ) {
1068 return $term;
1069 } elseif ( $output == ARRAY_A ) {
1070 return get_object_vars($term);
1071 } elseif ( $output == ARRAY_N ) {
1072 return array_values(get_object_vars($term));
1073 } else {
1074 return $term;
1075 }
1076 }
In line 1047, you can see that the taxonomy can be specified by the user, and is not harcoded than the other two functions previously mentioned
For your own benefit, you should dig into the links I have provided. Will help you a lot if you do.