0

我在前端有一个按钮,一旦点击它,登录和未登录的用户都会留下一条消息,我正在使用 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但再次没有运气。

请问有什么想法吗?

4

1 回答 1

1

您可以post_category用作函数的参数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',
    'post_category' => array(6)
   )
);
 `
于 2016-12-11T21:54:19.570 回答