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

发布于 2024-10-16 20:53:55 字数 1453 浏览 9 评论 0原文

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

add_action( 'publish_post', 'create_details_page');

仅当用户在某个类别中创建或更新帖子时才会创建自动帖子,并且自动帖子是在不同的类别中创建的。每个帖子只属于一个类别。 按如下方式创建帖子:

        $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 );

结果不一致:有时我会创建一个自动帖子,有时会创建两个,有时则不会。为什么以及如何只插入一次帖子?

要将自动帖子作为用户创建帖子的子帖子进行检索:

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

添加category_name 参数会导致根本不检索任何子帖子。如果没有类别参数,则返回子帖子,并且它们具有类别集属性。然而,似乎没有检索到完整的列表,并且每次运行的结果都不同。

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

关于如何解决不一致的行为并使其正常工作有什么建议吗? 我是 WordPress 新手,在线帮助很少,而且主要针对仪表板用户。

使用Wordpress 3.0.4、php5。

I need to programmatically create a post (or page) for each Wordpress post created or updated by a user via the dashboard. I added a hook

add_action( 'publish_post', 'create_details_page');

An auto post is created only if the user creates or updates a post in a certain category, and the auto post is created in a different category. Each post belongs to just one category.
Create the post as follows:

        $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 );

The results are inconsistent: sometimes I get one auto post created, sometimes two, and occasionally none. Why, and how to insert post exactly once?

To retrieve the auto post as a child of the user-created post:

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

Adding the category_name parameter causes to retrieve no child posts at all. Without the category parameter, child posts are returned, and they have the category set property. However, it seems like not a full list is retrieved, and results vary from run to run.

If the content of an auto post is then edited from the dashboard, this edited post is not returned by the above query. Why?

Any suggestions on how to resolve the inconsistent behavior and get this working?
I am new to Wordpress, and online help is sparse and mostly aimed to the dashboard user.

Using Wordpress 3.0.4, php5.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幼儿园老大 2024-10-23 20:53:55

检查 文档 - get_posts() 采用 category 参数,其中是类别ID。 category_name 参数用于不同的函数 query_posts()

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

Check the documentation - get_posts() takes an argument of category, which is the category ID. The category_name argument is used on a different function, query_posts().

To find the category ID, hover over the link to it on the WP categories backend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文