wp_insert_post not updating custom taxonomy selected if logged in as a subscriber

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

Related posts

1 comment

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

Comments are closed.