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 have a custom post type called q-questions, and it has a custom taxonomy that called q-categories.
I insert post via wp_insert_post and I want to define some terms for the taxonomy
My code is:

$my_post = array(
'post_title' => 'Custom-tax',
'post_content' => 'This is my post.',
'post_type' => 'q-questions',
 );

$post_id = wp_insert_post( $my_post );

What should I do to define the q-categories for this post as my-category?

share|improve this question
add comment

3 Answers

A category is just a taxonomy. And taxonomies are made up of terms. You'll need to set the post's terms via wp_set_post_terms.

This should be fairly simple:

wp_set_post_terms( '42', array( 'term', 'terms', 'terms' ), 'q-categories' );
share|improve this answer
    
I tried this wp_set_post_terms( $post_id, array( 'term', 'terms', 'terms' ), 'q-categories' ); but it doesn't work :( –  s976 Jun 10 '12 at 22:18
    
@s976 - Looking at the source, I can't see why this doesn't work. What error does wp_set_post_terms respond with? –  Stephen Harris Jun 10 '12 at 22:38
    
It responds with invalid_taxonomy –  s976 Jun 11 '12 at 18:41
    
I'm guessing you have register_taxonomy –  Tyler Carter Jun 12 '12 at 3:21
add comment

From the Codex:

$post = array(
   ...
  'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ]  
   ...
);  

So for your example:

$my_post = array(
  'post_title' => 'Custom-tax',
  'post_content' => 'This is my post.',
  'post_type' => 'q-questions',
  'tax_input'=>array(
     'q-categories' => array('my-category'),
   )
 );

$post_id = wp_insert_post( $my_post );
share|improve this answer
add comment

You have to register custom texonomy. Add below code in your functions.php. Ref

//hook into the init action and call create_question_taxonomies when it fires
add_action( 'init', 'create_question_taxonomies', 0 );

//create two taxonomies, genres and writers for the post type "question"
function create_question_taxonomies() 
{
  // Add new taxonomy, make it hierarchical (like categories)
  $labels = array(
    'name' => _x( 'q-categories', 'taxonomy general name' ),
    'singular_name' => _x( 'q-category', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search q-categories' ),
    'all_items' => __( 'All q-categories' ),
    'parent_item' => __( 'Parent q-categories' ),
    'parent_item_colon' => __( 'Parent q-categories:' ),
    'edit_item' => __( 'Edit q-categories' ), 
    'update_item' => __( 'Update q-categories' ),
    'add_new_item' => __( 'Add New q-categories' ),
    'new_item_name' => __( 'New q-categories Name' ),
    'menu_name' => __( 'q-categories' ),
  );     

  register_taxonomy('q-categories',array('question'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'q-categories' ),
  ));

}

Then you just add below code after wp_insert_post

wp_set_post_terms( $post_id, array( 'term', 'terms', 'terms' ), 'q-categories' );
share|improve this answer
add comment

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.