0

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?

3 Answers 3

1

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' );
4
  • I tried this wp_set_post_terms( $post_id, array( 'term', 'terms', 'terms' ), 'q-categories' ); but it doesn't work :(
    – Shimon S
    Commented Jun 10, 2012 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? Commented Jun 10, 2012 at 22:38
  • It responds with invalid_taxonomy
    – Shimon S
    Commented Jun 11, 2012 at 18:41
  • I'm guessing you have register_taxonomy Commented Jun 12, 2012 at 3:21
1

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 );
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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