以编程方式将子帖子插入Wordpress

jpr*_*ova 5 php wordpress parent-child taxonomy

我需要以编程方式为用户通过仪表板创建或更新的每个Wordpress帖子创建一个帖子(或页面)。我加了一个挂钩

add_action( 'publish_post', 'create_details_page');
Run Code Online (Sandbox Code Playgroud)

仅当用户在特定类别中创建或更新帖子,并且在其他类别中创建自动帖子时,才会创建自动帖子。每个帖子仅属于一个类别。创建帖子,如下所示:

        $auto_post = array(
                'comment_status' => 'closed',
                'post_category' => array($category->term_id),
                'post_author' => $latest_post[0]->post_author,
                'post_type' => 'post',
                'post_title' => 'Details for ' . $latest_post[0]->post_title,
                'post_parent' => $latest_post[0]->ID,
                'post_content' => 'Post content'
        );
        $auto_post_id = wp_insert_post ( $auto_post, true );
        $details = get_post( $auto_post_id );
        wp_publish_post( $auto_post_id );
Run Code Online (Sandbox Code Playgroud)

结果不一致:有时我创建了一个自动发布,有时创建了两个,有时却没有。为什么,以及如何精确地插入帖子一次?

要将自动发布作为用户创建的帖子的子代来检索:

$args = array(
        'post_type' => 'post',
        'post_parent' => $parent_post_id,
        'post_status' => 'publish'
        /* 'category_name' => array('Auto Post Category') */
);
$children = get_posts( $args );
Run Code Online (Sandbox Code Playgroud)

添加category_name参数将导致根本不检索任何子帖子。如果没有category参数,则返回子帖子,并且它们具有category set属性。但是,似乎未检索到完整列表,并且每次运行的结果都不同。

如果随后从仪表板编辑了自动发布的内容,则上面的查询不会返回此编辑过的帖子。为什么?

关于如何解决不一致行为并使此工作正常的任何建议?我是Wordpress的新手,在线帮助很少,主要针对仪表板用户。

使用Wordpress 3.0.4,php5。

Tap*_*eak 0

检查文档- get_posts() 接受一个参数category,它是类别 ID。该category_name参数用于不同的函数query_posts()

要查找类别 ID,请将鼠标悬停在 WP 类别后端的链接上。