简体   繁体   English

WordPress wp_insert_post没有插入标签

[英]WordPress wp_insert_post not inserting tags

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. 除了标签之外,Everythings工作正常,它不会插入任何标签。 Any idea? 任何想法?

Use the wp_set_object_terms() function: 使用wp_set_object_terms()函数:

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

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

Good luck 祝好运

Your post type is essays . 你的帖子类型是essays Custom post types do not support tags by default. 默认情况下,自定义帖子类型不支持标签。 You'll have to add a tags taxonomy to them. 您必须为它们添加tags分类。

http://codex.wordpress.org/Taxonomies http://codex.wordpress.org/Taxonomies

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

To insert the post with tags and categories do this 要插入带有标签和类别的帖子,请执行此操作

$pid=wp_insert_post($new_post);
wp_set_post_terms( $pid, $arrayoftags);
wp_set_post_categories( $pid, $arrayofcategories );

so $pid is the post id basically you first insert the post without tags or categories and the function returns the post's id which you can then use to insert the tags and categories each with their respective function, if you look at the source code of wp_insert_post you will notice that the function works in a different way for custom post types, I did not look more into it as I don't want to hack the code as there is a better solution by using the built in functions 所以$ pid是帖子ID,基本上你首先插入没有标签或类别的帖子,然后函数返回帖子的id,你可以用它来插入标签和类别,每个标签和类别各有各自的功能,如果你看一下wp_insert_post的源代码您会注意到该函数以不同的方式为自定义帖子类型工作,我没有看到它,因为我不想破解代码,因为使用内置函数有更好的解决方案

Hi I found this answer somewhere and this might help you out 嗨,我在某个地方找到了这个答案,这可能会帮助你

//first get the term (I used slug, but  you can aslo use 'name'), see: http://codex.wordpress.org/Function_Reference/get_term_by
$term = get_term_by( 'slug', 'your custom term slug', 'your custom taxonomy' );
//then get the term_id
$term_id = $term->term_id;
//Use 'tax_input' instead of 'post_category' and provide the term_id:
'tax_input' => array( 'your taxonomy' => $term_id )

Hope that helps. 希望有所帮助。

tags and post categories ought to be entered as an array, even if it's only one. 标签和帖子类别应该作为数组输入,即使它只是一个。 So 'tags_input' => 'TQM,tag' should be 'tags_input' => array('TQM,tag') 所以'tags_input' => 'TQM,tag'应该是'tags_input' => array('TQM,tag')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM