WordPress tax_input only if logged in

I am working on a wordpress plugin where a save lead data by forms in my website. When a user submits the form the following code will run so a post in post type “lead” will be added.

Almost everything works fine except the tax_input. When I am logged in in WordPress and submit the form in my website, the lead status “open” will be set.

Read More

But when I am not logged in in WordPress admin and submit the form (a visitor of website is never logged in off course) there is no lead status “open” set.

Does anyone know what I am doing wrong?

    // Get the id of taxonomy "lead-status" by slug
    $term = get_term_by('slug', 'open', 'lead-status' );
    $term_id = $term->term_id;  

    $new_lead = array(
        'post_title' => $lead_name,
        'post_content' => $lead_message,
        'post_type' => 'leads',
        'post_status' => 'publish',
        'tax_input' => array('lead-status' => $term_id)
    );

    $lid = wp_insert_post($new_lead);

Related posts

1 comment

  1. You can use wp_set_object_terms after the post is saved and in case it fails you will get reason for that ex:

    $new_lead = array(
        'post_title' => $lead_name,
        'post_content' => $lead_message,
        'post_type' => 'leads',
        'post_status' => 'publish',
    );
    
    $lid = wp_insert_post($new_lead);
    $status = wp_set_object_terms($lid,$term_id,'lead-status');
    

    Now this does the save but $status will hold the reason for failure in a failed case.

Comments are closed.