Leave a Reply

3 comments

  1. Use wp_set_object_terms after you have the post id for each taxonomy:

    ...
    $post_id = wp_insert_post( $my_post );
    wp_set_object_terms( $post_id, $location, 'location' );
    wp_set_object_terms( $post_id, $sale_rental, 'sale_rental' );
    wp_set_object_terms( $post_id, $price, 'price' );
    
  2. You can do it using wp_insert_post, but you must specify taxonomy as well in tax_input, so it should look like this:

    $item['tax_input'] = array (
        'location'      => implode( ',', $location ),
        'sale_rental'   => implode( ',', $sale_rental ),
        'price'         => implode( ',', $price ),
    )
    

    I use implode() so that $location could be an array with multiple terms.

    Also, notice that this works only for non-hierarchical taxonomies. For hierarchical taxonomies you must supply an array instead of astring.

  3. Source of the problem

    After some research to this topic, I was told to check the internals (which I did). As I were importing posts from an external feed as custom post type, I simply set the user to -1 (instead of adding a bot user). The problem I ran into was that wp_insert_post() with a tax_input set, internally checks for a user capability, which a non existing user obviously doesn’t have.

    SysBot for the rescue

    The solution then was that I wrote the SysBot plugin. This way I could simply attach the SysBot user (which has the role of editor) to that newly created post and everything worked the way it was expected.