Support » Plugins » get_cat_id is returning false, but how?

  • Mark Waterous


    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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • MichaelH


    Use get_cat_ID.



    delayedinsanity, PHP is case sensitive.

    Mark Waterous


    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.

    Was having the same problem ’til I realised it was due to using the langswitch plugin…

    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

    Today I had the same problem…

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

    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’);

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘get_cat_id is returning false, but how?’ is closed to new replies.