Attaching taxonomy data to post with wp_insert_post

I am trying to implement a front end posting system which shows taxonomy data in several dropdown select fields. Each of the dropdowns is named by using the “name” $arg in wp_dropdown_categories().

wp_dropdown_categories( array(
    'taxonomy'      => 'location',
    'hide_empty'    => 0,
    'orderby'       => 'name',
    'order'         => 'ASC',
    'name'          => 'location',
) );

As you can see taxonomy is “location” and the select name is also “location”.

I then add the variables for each of the taxonomy select dropdowns like so along with post_title, post_content etc:

$title          = trim( $_POST['wpuf_post_title'] );
$content        = trim( $_POST['wpuf_post_content'] );
$tags           = wpuf_clean_tags( $_POST['wpuf_post_tags'] );
$customcategory = trim( $_POST['customcategory'] );
$cat            = trim( $_POST['cat'] );
$location       = trim( $_POST['location'] );
$sale_rental    = trim( $_POST['sale_rental'] );
$price          = trim( $_POST['price'] );

Finally I add the extra info into an array ready to be sent by wp_insert_post(). I’m quite stuck on whether I am doing the right thing by adding tax_input into the array like below as this is what I understand from codex that I need to do.

'tax-input' => array( 
    $location,
    $sale_rental,
    $price
),

So that it all ends up looking like this:

$my_post = array(
    'post_title'    => $title,
    'post_content'  => $content,
    'post_status'   => $post_status,
    'post_author'   => $userdata->ID,
    'post_category' => array( $_POST['cat'] ),
    'post_type'     => $customcategory,
    'tags_input'    => $tags,
    'tax_input'     => array( 
        $location,
        $sale_rental,
        $price
    ),
);

$post_id = wp_insert_post( $my_post );

However, when I submitted the new post, all the standard post data (and also my custom post type) goes in ok but the taxonomies do not. I’m obviously doing something wrong but what?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 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' );

Solution 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.

Solution 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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply