【问题标题】:Wordpress : Working wrong - wp_insert_post for custom post type - category is not associatedWordpress:工作错误 - 自定义帖子类型的 wp_insert_post - 类别未关联
【发布时间】:2011-12-07 15:15:07
【问题描述】:

我设置了一个名为“照片”的自定义内容类型,它运行良好。现在我正在使用 wp_insert_post() 插入具有该内容类型的帖子,并且在添加类别时遇到问题。这些是我与帖子类型关联的分层类别,可以在管理区域中设置,但我尝试使用 PHP 代码关联它们,它不起作用。

这里是sn-p的代码:

$new_entry = array();
$new_entry['post_title'] = $_POST['submit-title'];
$new_entry['post_content'] = '';
$new_entry['post_status'] = 'pending';
$new_entry['post_type'] = 'photo';
$new_entry['post_author'] = $userID;
$new_entry['post_category'] = array(6,7); // this is the line that doesn't work
$new_entry['tags_input'] = array('nature','people','fun'); // this also doesn't work

// Insert the post into the database
$post_id = wp_insert_post( $new_entry );

这一切正常,自定义帖子已插入并按预期显示,但没有分配任何类别。 (类别 6 和 7 是应该与“照片”帖子类型相关联的类别)。不仅如此,这些标签也不会被创建并与这篇文章相关联。

有人对我做错了什么或他们推荐的其他方法有任何想法吗?

【问题讨论】:

    标签: wordpress post tags categories


    【解决方案1】:

    有一个函数,它叫做 wp_set_object_terms()。 http://codex.wordpress.org/Function_Reference/wp_set_object_terms

    例如:

    wp_set_object_terms( $post_id, $category_ids, 'category');
    

    您可以使用相同的功能添加标签

    【讨论】:

    • 不仅如此,这些标签也没有被创建并与这篇文章相关联。 - 你能帮我解决这个问题吗,实际上有些标签已经存在,有些是全新的,问题就在这里,感谢其他帮助
    猜你喜欢
    • 2011-06-24
    • 2013-07-14
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    相关资源
    最近更新 更多