Opened 14 years ago
Closed 14 years ago
#23109 closed defect (bug) (invalid)
Inconsistent returned value
| Reported by: | fp2x | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Taxonomy | Version: | 3.5 |
| Severity: | normal | Keywords: | |
| Cc: | Focuses: |
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)
#3
@
14 years ago
- Milestone Awaiting Review
- Resolution → invalid
- Status new → closed
get_cat_IDdoesn'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.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
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 useget_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.