您正在使用tax_input
不正确。你不应该这么说taxonomy_name
, 您应该输入自定义分类法的名称。看见https://codex.wordpress.org/Function_Reference/wp_insert_post. 所以,假设你的分类法叫做“忍者神龟”,你在帖子上贴上的标签是“多纳泰罗”和“米开朗基罗”:
// Create post object
$my_post = array(
\'post_title\' => \'My post\',
\'post_content\' => \'This is my post.\',
\'post_status\' => \'publish\',
\'post_author\' => 1,
\'tax_input\' => array( \'ninja_turtles\' => array( \'donatello\', \'michelangelo\' ) ) ,
\'post_category\' => array(8,39),
);
// Insert the post into the database
wp_insert_post( $my_post );
没有什么特别的原因(除了它让我觉得我有更细粒度的控制之外),我个人不喜欢使用
tax_input
但要使用独立功能
wp_set_post_terms()
https://codex.wordpress.org/Function_Reference/wp_set_post_terms 创建帖子后:
// Create post object
$my_post = array(
\'post_title\' => \'My post\',
\'post_content\' => \'This is my post.\',
\'post_status\' => \'publish\',
\'post_author\' => 1,
\'post_category\' => array(8,39),
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
// If all was successful, add the terms
if ( $post_id ) {
wp_set_post_terms( $post_id, array( \'donatello\', \'michelangelo\' ), \'ninja_turtles\' );
}