我有一个自定义的帖子类型,叫做eyeglasses
和自定义分类法models
. 我还为分类学创建了两个术语,称为M156
和M120
. 现在我正试图上传两篇文章以供测试通过wp_insert_post
.
这是添加$title
到post_title
属于eyeglasses
但不增加或更新该职位的任期。
function we_load_posts($title, $term)
{
wp_insert_post(array(
"post_type" => "eyeglasses",
"post_title" => $title,
"tax_input" => array(
"models" => array($term)
),
"post_status" => "publish"
));
}
we_load_posts("Montana", "M156");
we_load_posts("Havana", "M120");
你能告诉我我错过了什么或做错了什么吗?
最合适的回答,由SO网友:Nazaria 整理而成
我想到几点:
有一个输入错误"post_type" => "\'eyeglasses"
(额外单引号)。它应该是:"post_type" => "eyeglasses"
.
试着把$term
而不是array( $term )
:
"tax_input" => array(
"models" => $term
)
还有,是吗
models
或
model
? e、 g。
"tax_input" => array(
"model" => $term
)
tax_input
需要assign_terms
能力。因此,如果与您一起运行此代码的用户不具备该功能,则无法正常工作。在这种情况下,正确的方法是:
$post_id = wp_insert_post(array(
"post_type" => "eyeglasses",
"post_title" => $title,
"post_status" => "publish"
));
wp_set_object_terms( $post_id, $term, \'model\' );