[英]wp_insert_post allow to set taxonomy categories for non logged in users - wordpress
我在前端有一个按钮,一旦单击它既登录又未登录,用户留下了一条消息,而我正在使用wp_insert_post()函数添加新消息,这是我的代码:
$post_id = wp_insert_post(array (
'post_type' => 'messages',
'post_title' => $title,
'post_content' => $message,
'post_excerpt' => $excerpt,
'post_status' => 'publish',
'comment_status' => 'closed',
'post_author' => 1,
'ping_status' => 'closed',
'tax_input' => array(
'message-category' => array(26),
)
));
但是除了添加新帖子外,我还需要设置分类类别。 但是,仅当用户登录时才设置类别。对于未登录的用户,它不起作用。 我试图添加post_author(检查以上内容),但这仍然无济于事。
还尝试使用下面的功能,但是再次,没有登录的用户留下消息时,没有设置运气和类别:
wp_set_object_terms( $post_id, array(26), 'message-category', true );
并且也尝试了post_category
但再次没有运气。
有什么想法吗?
您可以将post_category
用作wp_insert_post
函数的参数。 请参考doc 。
您的功能应如下所示:
`
$post_id = wp_insert_post(array (
'post_type' => 'messages',
'post_title' => $title,
'post_content' => $message,
'post_excerpt' => $excerpt,
'post_status' => 'publish',
'comment_status' => 'closed',
'post_author' => 1,
'ping_status' => 'closed',
'post_category' => array(6)
)
);
`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.