Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using fornt end form submission now i want to add new category and before adding this category i want to check whether it exist or not, if yes then simple add to that category otherwise add new one. So the in codex they said if you have to add catogory just use the


and there is one more function to add this which is


and there is one more way of adding category is


so there is confussion in both how they work and which one i chose form them. I have name of category so can i use it now. There is code how to add them but they are not working for me.

//Check if category already exists
$cat_ID = get_cat_ID( $category );

//If it doesn't exist create new category
if($cat_ID == 0) {
$cat_name = array('cat_name' => $category);

//Get ID of category again incase a new one has been created
$new_cat_ID = get_cat_ID($category);

// Create post object
$new_post = array(
'post_title' => $headline,
'post_content' => $body,
'post_date' => $date,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array($new_cat_ID)

So any solution for that. Thanks

share|improve this question
Please add debugging information –  s_ha_dum Mar 28 '13 at 13:09

1 Answer 1

$id = wp_create_category('My category name');

By above function category created & categary id is returned.

If the category already exists, it is not duplicated. The ID of the original existing category is returned without error.

You can use $id as category id.



share|improve this answer
When i use this funtion, my all page stuck and display nothing. –  Adi Mar 28 '13 at 12:52
wp_create-category is a valid function but is a "thin wrapper" around the wp_insert_category function, so it isn't substantially different from what the OP has tried. –  s_ha_dum Mar 28 '13 at 13:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.