0

I use the code like below in a submit page.

$new_post = array(
'post_title'    =>  $title,
'post_content'  =>  $description,
'post_excerpt'  =>  $excerpt,
'post_category' =>  array($_POST['cat']),
'tags_input'    =>  array($tags),
'post_status'   =>  'publish',          
'post_type' =>  'custom_post_type'  
);
$pid = wp_insert_post($new_post);

The $_POST['cat'] fetches the correct `category id. My custom taxonomy is named directory.

I am able to view in the saved post in the backend but the category checkbox is not checked there and in the front end i am displaying the post under these custom category, but it doesn't appear.

Is there a way to save the custom category of a custom taxonomy properly.

Mohamed Salah
  • 959
  • 10
  • 40
Manoj M
  • 82
  • 6

1 Answers1

1

To assign a taxonomy to a post:

    $cat_ids = array( $new_cat_id );
    $cat_ids = array_map('intval', $cat_ids);
    wp_set_object_terms( $post_id, $cat_ids, 'my_tax_name' );
wesamly
  • 1,484
  • 1
  • 16
  • 23