Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress wp_insert_post not inserting tags

Tags:

php

wordpress

I am trying to insert a post with this code:

$my_post = array(
                'post_type'    => "essays",
                'post_title'    => 'TEST 3',
                //'post_content'  => $content,
                'post_status'   => 'draft',
                'post_author'   => 1,
                //'post_category' => $cat,
                'tags_input'    => 'TQM,tag',
        );

$post_id = wp_insert_post($my_post);

Everythings works ok except the tags, it does not insert any of them. Any idea?

like image 711
yedidel Avatar asked May 03 '12 15:05

yedidel


2 Answers

Use the wp_set_object_terms() function:

http://codex.wordpress.org/Function_Reference/wp_set_object_terms

wp_set_object_terms($post_id , $arrayoftags, $name_of_tag_taxonomy, false);

Good luck

like image 155
Adib Aroui Avatar answered Oct 17 '22 10:10

Adib Aroui


Your post type is essays. Custom post types do not support tags by default. You'll have to add a tags taxonomy to them.

http://codex.wordpress.org/Taxonomies

http://codex.wordpress.org/Function_Reference/register_taxonomy

like image 4
Nadh Avatar answered Oct 17 '22 08:10

Nadh