1

get_category is returning null but using get term_by or get_categories() is not. Why?

I have created term called categorycourses. I have a post related to coursecategory Lidköping (vardump1),

vardump1

object(stdClass)[2525]
  public 'term_id' => &int 5
  public 'name' => &string 'Lidköping' (length=10)
  public 'slug' => &string 'kurser_lidkoping' (length=16)
  public 'term_group' => int 0
  public 'term_taxonomy_id' => int 105
  public 'taxonomy' => string 'category' (length=8)
  public 'description' => &string '' (length=0)
  public 'parent' => &int 0
  public 'count' => &int 1
  public 'filter' => string 'raw' (length=3)
  public 'cat_ID' => &int 5
  public 'category_count' => &int 1
  public 'category_description' => &string '' (length=0)
  public 'cat_name' => &string 'Lidköping' (length=10)
  public 'category_nicename' => &string 'kurser_lidkoping' (length=16)
  public 'category_parent' => &int 0

vardump2

null

vardump3

object(stdClass)[2525]
  public 'term_id' => int 105
  public 'name' => string 'Skara' (length=5)
  public 'slug' => string 'kurser_skara' (length=12)
  public 'term_group' => int 0
  public 'term_taxonomy_id' => int 108
  public 'taxonomy' => string 'categorycourses' (length=15)
  public 'description' => string 'skara' (length=5)
  public 'parent' => int 0
  public 'count' => int 1
  public 'filter' => string 'raw' (length=3)

I see that taxonomy returned by get_category is 'category', while taxonomy returned by get_category is 'categorycourses'.

Here's the code I'm using:

//Vardump1
$location = get_category(skovdeLocationId); //another location id
var_dump($location);     

//Vardump2
$location = get_category(skaraLocationId, 'categorycourses');
var_dump($location); //returns null (with or without taxonomy defined)

//Vardump3
$location = get_term_by( 'id', skovdeLocationId, 'categorycourses' );
var_dump($location);

But

get_categories(array(
        'taxonomy'  =>'categorycourses',
        'include'   => skaraLocationId
        ));

is returning term correctly (as vardump1 but where location = skaraLocationId)

Why? Shouldn't get_categories and get_category be the same? (or get_termby)

2 Answers 2

2

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.

1
  • Aaah of course - about the get_category() that only returns the "category-part" and not the actual term. Thanks a lot!! Jun 12, 2014 at 10:54
2

First of all get_categories retrieves a list of categories, and it shouldn't be used for fetching a single category by ID.

The different between get_term_by and get_category is that get_term_by allows you to specify a taxonomy, where get_category always uses the taxonomy category. Even though the function name would suggest otherwise, get_categories also allows you to fetch from other taxonomies than category.

So, get_categories and get_category shouldn't always return the same term (even if it's in an array for the former), as get_category only allows you to get terms of the category taxonomy, and get_categories supports other taxonomies as well. The same goes for get_term_by.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.