Not Able to Insert Taxonomy Term Using wp_insert_post()

Few points come to mind:

  1. There is a typo in "post_type" => "'eyeglasses" (extra single quote). It should be: "post_type" => "eyeglasses".

  2. Try putting the $term instead of array( $term ):

    "tax_input" => array(
        "models" => $term
    )
    
  3. Also, is it models or model? e.g.

    "tax_input" => array(
        "model" => $term
    )
    
  4. tax_input requires assign_terms capability. So if the user you are running this CODE with, doesn’t have that capability, it’ll not work.

    In that case, the right way is:

    $post_id = wp_insert_post(array(
        "post_type" => "eyeglasses",
        "post_title" => $title,
        "post_status" => "publish"
    ));
    
    wp_set_object_terms( $post_id, $term, 'model' );
    

Leave a Comment