根据类别和位置附加帖子 - Wordpress

时间:2018-01-07 19:14:31

标签: php wordpress

我正在使用wordpress网站,其中帖子已正确附加,明显有意义且所有帖子都有序,但在数据库中没有任何帖子连接的引用。这里有成千上万的帖子,所以我只展示一个我正在使用的例子,以及我希望通过动作钩子或函数实现的目标。甚至只是一个update_post方法。

目前的帖子示例列表

Post Title 1 (Category = Folder)
Post Title 2 (Category = Item)
Post Title 3 (Category = Item)
Post Title 4 (Category = Item)
Post Title 5 (Category = Item)
Post Title 6 (Category = Folder)
Post Title 7 (Category = Item)
Post Title 8 (Category = Item)
Post Title 9 (Category = Item)
Post Title 10 (Category = Item)

我想要达到的目标是:

类别'项目'中的任何帖子在类别'文件夹'中的帖子下方然后成为上述文件夹帖子的孩子,所以我的帖子会变成这样:

Post Title 1 (Category = Folder)
- Post Title 2 (Category = Item Post_parent = Post Title 1)
- Post Title 3 (Category = Item Post_parent = Post Title 1)
- Post Title 4 (Category = Item Post_parent = Post Title 1)
- Post Title 5 (Category = Item Post_parent = Post Title 1)

Post Title 6 (Category: Folder)
- Post Title 7 (Category = Item Post_parent = Post Title 6)
- Post Title 8 (Category = Item Post_parent = Post Title 6)
- Post Title 9 (Category = Item Post_parent = Post Title 6)
- Post Title 10 (Category = Item Post_parent = Post Title 6)

我想知道是否有人可以让我自己走上正确的道路,我可以自己编写脚本。有点像:

对于类别链接中的每个帖子>这是在类别文件夹>中的帖子下面使这篇文章成为上面文件夹类别中帖子的孩子

我正在努力参考的内容是'这是一个帖子'。这可以通过使用已发布的发布日期来引用吗?

1 个答案:

答案 0 :(得分:0)

首先,wordpresss post不是分层的,所以你可能想要提供层次结构支持,这样你就可以在后端看到它们,你可以添加这样的东西

add_action('registered_post_type', '_enable_post_hierarchy', 10, 2 );
function _enable_post_hierarchy( $post_type ) {

    if ($post_type !== 'post') return;
    global $wp_post_types;
    $wp_post_types['post']->hierarchical = 1;
    add_post_type_support( 'post', 'page-attributes' );

}

关于你的主要关注点,你可以实现你想要的;

首先; 创建自定义WP_Query,查询所有帖子(虽然我认为你应该实施分页,因为你有很多帖子)与ITEM类别,并确保订单与你上面说的相同,我相信它orderby=dateorder=DESC

然后; 获得结果后,循环遍历每个结果并执行另一个查询,这次,它只需要一个结果,类别为FOLDER和/或不在类别ITEM中,最重要的部分是POST_DATE必须大于当前项目POST_DATE(来自您的循环),您可以创建自定义SQL查询或尝试使用WP_Query

另一种方法是使用get_adjacent_post内置函数,这可以在wordpress的下一篇和上一篇文章中使用,但如果你有其他类别而不是ITEM和FOLDER,这将无法使用。看看https://codex.wordpress.org/Function_Reference/get_adjacent_post

这是我提出的一些未完全测试的代码

首先让我们尝试获取数据并在前端显示数组,以验证我们的新数据阵列是否构建正确

// spit the data to verify the order and hierarchy
add_action('wp_head', function() {
    echo '<pre>', print_r( _check_data_post(), 1), '</pre>';
});

function _check_data_post( $display = 'data' ) {

    // ID of the item category
    $item_id = 29;

    //create query argument
    $args = [
        'post_type'     => 'post',
        'post_status'   => 'publish',
        'orderby'     => 'date',
        'order'       => 'DESC',
        'cat'         => $item_id,

        // lets add some limit not to kill the server
        'post_per_page' => 50,

    ];

    //build new query
    $q = new WP_Query( $args );

    //get all query result
    $posts = $q->get_posts();

    //new data holder
    $data = [];

    // loop through each result
    foreach ($posts as $key => $new_post) {

        // the global $post is needed for get_adjacent_post to work
        global $post;

        // assign each result to $post
        $post = $new_post;

        // get the next post, the parameters is as follows
        // false - must not be only in same terms, meaning its not gonna query in ITEM category only but rather query all category
        // $item_id - exclude the specified ID, we must not include the ITEM category
        // false - (true means previous) NEXT to the current $post which is the current item of our loop, meaning it must have greater date than our current item

        // This will not work if you have categories other then Folder and Item
        // You should write your own custom SQL to be more specific and adjust on your need
        $parent = get_adjacent_post( false, $item_id , false );


        if( $display == 'ids' ) {
            // lets return each post ID with additional "parent" ID
            $data[$key]['id']       = $post->ID;
            $data[$key]['parent']   = $parent->ID;
        } else {

            // Display Title and Category instead of the ID to verify the result
            $data[$key]['cat']      = get_the_category( $post->ID )[0]->name;
            $data[$key]['title']    = $post->post_title;
            $data[$key]['parent']   = [
                'cat'       => get_the_category( $parent->ID )[0]->name,
                'title' => $parent->post_title,
            ];
        }
    }

    return $data;
}

一旦验证完毕,我们将遍历每个项目并使用新的父帖子ID更新每个项目,您可以在安全的地方调用此函数并且只运行一次,它还可以更好地添加一堆条件验证和错误消息< / p>

function _update_data_post() {

    // get the all the item, must be IDs only
    $list_items = _check_data_post('ids');

    // loop through each item
    foreach ( $list_items as $items ) {

        //assign item data
        $data = [
            'ID'             => $items['id'],
            'post_parent'   => $items['parent'],
        ];

        //update item with assigning post_parent value
        wp_update_post( $data );
    }
}