1

Here is my Code, it works fine When i try to post data from front end if Logged in as admin, but if i logged in as a subscriber code works fine but it is not inserting the taxonomy term i select, here is the code..

  $new_post = array(
    'post_title' => $postTitle,
    'post_content' => $post,
    'post_status' => 'publish',
    'post_date' => date('Y-m-d H:i:s'),
    'post_author' => $user_ID,
    'post_type' => 'publications',
    'tax_input' => array( 'publicationstype'=> $term_id ) 
    //'post_category' => array(6)
);

wp_insert_post($new_post);

here 'publicationstype' is the custom taxonomy, is there any one, who can help me!! thanx in advance

1 Answer 1

2

It's because within wp_insert_post current user capabilities are checked before adding the terms:

if ( current_user_can($taxonomy_obj->cap->assign_terms) )
    wp_set_post_terms( $post_ID, $tags, $taxonomy );

to get around this, use wp_set_object_terms instead after wp_insert_post to add the terms:

$new_post = array(
    'post_title' => $postTitle,
    'post_content' => $post,
    'post_status' => 'publish',
    'post_date' => date('Y-m-d H:i:s'),
    'post_author' => $user_ID,
    'post_type' => 'publications'
);
$new_id = wp_insert_post( $new_post );
wp_set_object_terms( $new_id, $term_id, 'publicationstype' );
2
  • Thanx Brother, it works
    – Salih K
    Commented May 15, 2013 at 9:48
  • Yeah thx for that
    – 4ndro1d
    Commented Jan 29, 2014 at 21:44

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.