Ready to get started?Download WordPress


get_cat_id is returning false, but how? (8 posts)

  1. Mark Waterous
    Posted 4 years ago #

    I'm having a problem with get_cat_id returning 0/false on categories I know exist and also have posts in them.. I even ran a test, using the category slug provided to get the id, and it doesn't work either.

    The test code looks like;

    $vid = get_cat_id('vendors');
    $categories = get_categories('child_of=' . $vid . '&hide_empty=0');
    foreach($categories as $category) {
    	echo get_cat_id($category->slug) . '<br />';

    ...returns a bunch of 0's. Any ideas why this isn't working?

  2. MichaelH
    Posted 4 years ago #

    Use get_cat_ID.

  3. s_ha_dum
    Posted 4 years ago #

    delayedinsanity, PHP is case sensitive.

  4. Mark Waterous
    Posted 4 years ago #

    Thanks guys, but I'm not new to PHP. :) PHP variables and member properties are case sensitive, functions are not. That is why the following code;

    function test () {
        echo 'test';
    function Test () {
        echo 'Test';

    Will return; Fatal error: Cannot redeclare test() (previously declared in ***.php:1) in ***.php on line 3

    So not the issue, though I'll make sure I double check my semantics in the future. In the meantime I've discovered using get_cat_ID was pointless anyways as inside of the foreach loop I had access to $category->cat_ID.

    Nonetheless I still get some odd business with get_cat_ID() - I turned on paging for my custom loop, and for some reason previous_posts_link() was working but next_posts_link() wasn't. So I took the body of my function which created and displayed the custom loop, and placed it directly in the page just to see if there was a global missing somewhere. Upon doing so, get_cat_ID failed again, this time for gawd knows what reason.

    Not sure, but I think I'll start by looking at the functions body itself.

  5. shahar
    Posted 4 years ago #

    Was having the same problem 'til I realised it was due to using the langswitch plugin...

  6. MichaelH
    Posted 4 years ago #

    This works just fine using the WordPress Default theme and putting this code

    echo 'this is the category id for uncategorized: '. get_cat_ID('uncategorized');

    just after <div id="content" class="narrowcolumn" role="main"> in wp-content/themes/default/index.php

  7. relvao
    Posted 4 years ago #

    Today I had the same problem...

    You must use the category name for this function, not the slug.

  8. pantsonhead
    Posted 4 years ago #

    relvao is correct, you must use the category name, not the slug.

    Also, this function will NOT return the ID if your category name contains "&".

    example: get_cat_ID('Bits & Pieces');

Topic Closed

This topic has been closed to new replies.

About this Topic