wp_insert_category() setting the ‘cat_ID’ gives not array error

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.

Read More
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!

Related posts

2 comments

  1. 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.

  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'=>''
      )
    );
    

Comments are closed.