Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am implementing (trying) 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.

<?php wp_dropdown_categories('taxonomy=location&hide_empty=0&orderby=name&order=asc&name=location') ?>  

as you can see taxonomy is "location" and select name is "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']);

and finally i add the extra info into an array ready to be sent by wp_insert_post, im quite stuck on whether i am doing the right thing by adding tax_input into the array like below as this is what i seem to be understanding 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, im obviously doing something wrong, but what?

share|improve this question
add comment

3 Answers 3

up vote 11 down vote accepted

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');
share|improve this answer
    
I just do not know where i would be without your help, worked first time... many thanks Bainternet. Marked as answered –  MartinJJ May 25 '11 at 14:45
    
Glad i could help :) –  Bainternet May 25 '11 at 14:50
add comment

you can do it using wp_insert_post, but you must specify taxonomy as well in tax_input, so it should look like

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

(i use implode, so that $location ... could be arrays with multiple terms)

Also notice, that this work only for non-hierarchical taxonomies, for hierarchical, you must supply an array, not string

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.