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'm writing a plugin that automatically creates a bunch of stuff ie categories and child categories. But when I use the following code to create the categories: (from wp codex)

//Create Post Categories
$my_cat = array(
    'cat_ID' => 1146,
    'cat_name' => 'Newcastle Community News & Views', 
    'category_description' => '', 
    'category_nicename' => 'news', 
    'category_parent' => '',
    'taxonomy' => 'category'
);
$my_cat_id = wp_insert_category($my_cat);

It gives me the following error.

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in /home/newcastl/public_html/wp-includes/taxonomy.php on line 2535

With the id in '' isnt working either. I have to set the ID so I can refer to it when using sub-cats. Thanks!

share|improve this question
    
what i found out so far: when i use 'cat_ID' => array(3352), it doenst give me the error msg but it creates a category with the ID 1. Probably because there is one value in the array. Any ideas? –  karunin Dec 19 '13 at 5:31
    
If you are creating a category, why you are passing the cat_ID value? –  Chittaranjan Dec 19 '13 at 7:09
    
Yeah I actually realized that it is not possible to give an id if you create a new category. The codex is not so clear on this and I didn't know. I have a solution now and will post as soon as the system lets me. –  karunin Dec 19 '13 at 7:34

2 Answers 2

Try to use wp_insert_term() in place of wp_insert_category(). You can use the following code:

wp_insert_term(
 'Newcastle Community News & Views', 
 'category', 
  array(
     'description'=>'',
     'slug'=>'',
     'parent'=>''
  )
);
share|improve this answer
    
I tried this, but it doesn't work either. You are not supposed to give id's when creating new stuff. The codex is not so clear on this and I didn't know. I have a solution now and will post as soon as the system lets me. –  karunin Dec 19 '13 at 7:36

Ok here is the solution I found: You are not meant to set IDs when creating new objects like categories. Instead I created the parent categories first using something like

$my_cat = array(
    'cat_name' => 'Community News & Views', 
    'category_nicename' => 'news', 
    'taxonomy' => 'category'
);
$my_cat_id = wp_insert_category($my_cat);

In a second step I create the child categories using

    $parent_term = term_exists( 'news', 'category' );
$parent_term_id = $parent_term['term_id'];
$my_cat = array(
    'cat_name' => 'Business Articles', 
    'category_nicename' => 'business-articles', 
    'category_parent' => $parent_term_id,
    'taxonomy' => 'category'
);
$my_cat_id = wp_insert_category($my_cat);

Note: You cannot create Parent and Child categories at the same time because the parent category has to exist, when you create the Child category Note also: term_exists( 'news', 'category' ); uses the $slug as first argument. The codex is not so clear on that one.

share|improve this answer

Your Answer

 
discard

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.