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'm creating new categories with galleries taxonomy :

$wpdb->insert("wpem_terms", array(
    'name'=>'Custom Category',
    'slug'=>'customcategory',
    'term_group'=>0,
));

//id of term
$id = $wpdb->insert_id;

$wpdb->insert("wpem_term_taxonomy", array(
    'term_id'=>$id,
    'taxonomy'=>'galleries',
    'description'=>'',
    'parent'=>'',
    'count'=>'',
));

It's working. And then i'm trying to publish a new post with this newly created category :

wp_insert_post(array(
    'post_author'=>1,
    'post_date'=>date('Y-m-d H:i:s'),
    'post_date_gmt'=>date('Y-m-d H:i:s'),
    'post_content'=>'',
    'post_title'=>'',
    'post_excerpt'=>'',
    'post_status'=>'publish',
    'comment_status'=>'closed',
    'post_name'=>'',
    'post_parent'=>0,
    'post_type'=>'portfolio', // with 'portfolio' custom post type
    'post_category'=>array($id),

));

This is publishing post , it's OK but post's category isn't Custom Category . I think this line invalid :

'post_category'=>array($id),

Any ideas? Also i tried this :

'post_category'=>array(get_cat_ID("Custom Category")),
share|improve this question
    
Your syntax looks correct. Echo $id just to make sure it is set properly. –  imHavoc Jan 17 '12 at 16:05
    
@imHavoc , it's printing right ID . There isn't any problem . –  Eray Jan 17 '12 at 16:07
    
If you use this: 'post_category'=>array(intval($id)), do you still get the same problem? –  Jared Jan 17 '12 at 16:38
    
Yes. I've tried array(intval($id)) –  Eray Jan 17 '12 at 16:41
add comment

1 Answer

up vote 1 down vote accepted

Try doing this:

$insert_post = wp_insert_post(array(
    'post_author'=>1,
    'post_date'=>date('Y-m-d H:i:s'),
    'post_date_gmt'=>date('Y-m-d H:i:s'),
    'post_content'=>'',
    'post_title'=>'',
    'post_excerpt'=>'',
    'post_status'=>'publish',
    'comment_status'=>'closed',
    'post_name'=>'',
    'post_parent'=>0,
    'post_type'=>'portfolio', // with 'portfolio' custom post type
    'post_category'=>array($id),
));
if( $insert_post ) {
    wp_set_object_terms( $insert_post, intval( $id ), 'galleries');
}

Using wp_set_object_terms you can set the terms for the post in any taxonomy. The first argument is the ID of the post, the second argument is the ID or slug of the term, and the third is the name of the taxonomy. If you use an ID for the term, you must use intval() otherwise it will create a term that is just a number.

share|improve this answer
    
wp_set_object_terms' first argument is $insert? or $insert_post ? –  Eray Jan 17 '12 at 16:44
    
Sorry, it would be $insert_post, must have missed that. –  Jared Jan 17 '12 at 16:45
    
not working : wp_set_object_terms( $insert_post, intval( $id ), 'customcategory'); or wp_set_object_terms( $insert_post, intval( $id ), 'Custom Category'); –  Eray Jan 17 '12 at 16:49
    
Leave the word 'category' as-is in the code to set the category, the third argument for wp_set_object_terms is the type of taxonomy that is being added to the post. –  Jared Jan 17 '12 at 16:51
1  
Updated my answer, glad I could be of assistance. :) –  Jared Jan 17 '12 at 17:03
show 3 more comments

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.