• vestaviascott

    (@vestaviascott)


    I’m seeking to test for the existence of a category (by name) and if the category does not exist, I’d like to create it.

    I’m using the following code in my theme’s functions.php file…

    if(get_cat_ID('hidden')!==1){wp_create_category('hidden');}

    However, its returning an undefined function error. I suppose I need to include taxonomy.php in my functions.php?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I thought the wp_create_category is already checking if the category exists.

    yes wp_create_category will return the existing category id or false.

    Remember the passed category name must be an exact match.

    Download and Downloads would result in 2 categories.

    Contact WebTechGlobal for more support

    No need to add the compare, as get_cat_ID returns ‘0’ or the catagory id if found, and as 0 will be seen by the code as false you could use if not ! .

    if(!get_cat_ID('hidden')){wp_create_category('hidden');}

    If that fails let me know as I have a function that will work for this situation, I use in my themes to add default categories, as I wanted to be able to add the parent category by name as well so I created my own function as wp_create_category needs the parent ID and will not accept a name.

    David

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp_create_category() from functions.php returns Undefined’ is closed to new replies.