Make WordPress Core

Opened 11 years ago

Closed 11 years ago

#23109 closed defect (bug) (invalid)

Inconsistent returned value

Reported by: fp2x's profile fp2x Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.5
Component: Taxonomy Keywords:
Focuses: Cc:

Description

I have created a category (its slug is 'misc') with WP 3.4.2.
I have updated to WP 3.5.

Now, when I try to get the ID of the 'misc' category created with WP 3.4.2, I get different results from different functions.

    $t=get_category_by_slug( 'misc' );
    echo $t->term_id;// 18
    echo get_cat_ID('misc');// 0 but should be 18

After looking at category.php (http://core.trac.wordpress.org/browser/tags/3.5/wp-includes/category.php#L0), it seems that the two functions behave differently.

get_cat_ID doesn't take avantage of _make_cat_compat.

It may look like this :

function get_cat_ID( $cat_name ) {
   $cat =  get_category_by_slug( $cat_name );
   if ( $cat )
          return $cat->term_id;
   return 0;
}

Change History (3)

#1 @alexvorn2
11 years ago

  • Component changed from General to Taxonomy

#2 @ocean90
11 years ago

You shouldn't mix up the slug of the cat and the name of the category.

get_cat_ID() returns the ID from the category name, not the slug. If you want the ID from a category slug you have to use get_category_by_slug().

Also I couldn't reproduce the issue you have described. Even it's the slug get_cat_ID() returns the correct ID, tested in 3.4 and 3.5.

#3 @SergeyBiryukov
11 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

get_cat_ID doesn't take avantage of _make_cat_compat.

It doesn't need to. _make_cat_compat() just fills up some legacy category fields:
http://core.trac.wordpress.org/browser/tags/3.5/wp-includes/category.php#L301

It makes sense for get_category_by_slug(), which returns the full object, but not for get_cat_ID(), which only returns the ID.

I can reproduce getting 0 when passing a slug rather than the name to get_cat_ID(). But that's the expected result if there's no category by that name. If the name actually matches the slug, get_cat_ID() returns the proper ID. This behaviour is the same in 3.4 and 3.5.

Note: See TracTickets for help on using tickets.