首页 > 解决方案 > 2 自定义 wp_query 使用与 'orderby' => 'rand' 相同的 rand?

问题描述

在我的主页上,我显示了 9 个帖子,我有一个加载更多按钮(带有 ajax),它调用了 9 个其他帖子等等。目前我按“日期”订购,但我想随机订购。问题是我的第一个 wp_query orderby rand 显示 9 个随机帖子,当我加载更多帖子时,我的第二个 wp_query orderby random 也可以显示前 9 个帖子中的相同帖子。

如何使用相同的随机数随机化 2 wp_query?还是有其他方法可以做到这一点?


正如社区成员已经建议的那样,我尝试将可见 ID 存储在一个数组中并使用post__not_in如下:

$query = new WP_QUERY(array(
        'post_type' => 'my_posts',
        'posts_per_page' => 9,
        'orderby' => 'rand',
        'post__not_in' => $postNoIn,
        'paged' => $paged

    ));

但这并没有解决我的问题。

$paged变量是我的帖子的当前页码。我有 32 篇文章,我按 9 篇 9 篇的篇幅发布,所以有 4 页。当我使用上面显示的第二个 wp_query 时,我得到 9 个随机帖子减去可能的 9 个第一个帖子,所以我可以有少于 9 个帖子,并且在到达最后一页时查询停止。我将有大约 28 个帖子,而不是 32 个,因为查询在第 4 页停止,直到显示所有帖子。

提前感谢您的回答!

我终于成功了! 这是我的答案

我更改了我的第一个查询以通过“rand”获取所有邮政订单,例如:

$args = array(
        'post_type' => 'my_posts',
        'posts_per_page' => -1,
        'orderby' => 'rand'
    );

然后在我的循环中我显示 9 个第一个帖子,在 9 个帖子之后我得到所有其他帖子的 ID

<?php
$index = 0;
$idsInt = array();

while ($the_query->have_posts()):
$the_query->the_post();

    if ($index < 9) : 

        get_template_part('part-template/content', get_post_format());
        $index++;

    else :

        array_push($idsInt, get_the_ID());

    endif; 

endwhile;

之后,我准备了要与 ajax 一起使用的数据,为此,我将int元素列表转换为string列表

$index = 0;
$idsString = array();

foreach ($idsInt as $id) {
    if ($index == 0) {
        $idsString .= " \"" . $id . "\"";
    } else {
        $idsString .= ", \"" . $id . "\"";
    }
    $index++;
}

这是我的“加载更多”按钮,我输入了“data-id”我的 ID 列表

<div class="text-center">
    <a id="btn-load-more" data-id='[<?php echo $idsString ?>]' 
        class="home-load-more"
        data-page="1"
        data-url="<?php home_url(); ?>/wp-admin/admin-ajax.php" >
        <span class="text"><?php _e('Load More') ?></span>
    </a>
</div>

在我的 script.js 中,我的 ajax 函数如下所示:

$(document).on('click', '.home-load-more', function () {

    let that = $(this);
    let page = that.data('page');
    let newPage = page + 1;
    let ajaxurl = that.data('url');
    let ids = that.data('id');

    $.ajax({

        url: ajaxurl,
        type: 'post',
        data: {

            page: page,
            action: 'home_load_more',
            foo : ids

        },
        error: function (response) {

            console.log(response);

        },
        success: function (response) {

            if (!(response === "")) {

                that.data('page', newPage);
                $('.my_row').append(response);

            }
        }
    });
});

My$_POST['page']包含在每次 Load More 调用时都会增加的页面数量,稍后我将解释为什么要使用它,并且 my$_POST['foo']包含在我的第一个查询循环中创建的 ID 列表。

现在我的php函数! (我的代码在 php 注释中的解释!)

function home_load_more()
{

    $ids = ($_POST['foo']);
    $paged = $_POST["page"];
    $postIn = array();

    // I want to display only 9 posts on each calls
    for ($i = 0; $i < 9; $i++) {

        /* Here is my strange calcul to get ids 
         - from 0 to 8 on the first call
         - from 9 to 17 on the second call
         - from 18 to 26 on the third call
         and so on... 
         That is why I use the $paged, to get
         a value that is increment by one on every calls. */

        // Break if they are no more ids in the list
        if ($ids[($paged - 1) * 9 + $i] == null) {
            break;
        }

        // $postIn contains 9 posts IDs
        array_push($postIn, $ids[($paged - 1) * 9 + $i]);
    }

    $query = new WP_QUERY(array(
        'post_type' => 'my_posts',
        'orderby' => 'post__in',   // here I give my list of 9 ids
        'post__in' => $postIn
    ));

    if ($postIn[0] == null) {
        // Load More button won't display any posts
        wp_reset_postdata();
    } else {
        if ($query->have_posts()) {

            while ($query->have_posts()) {
                $query->the_post();
                get_template_part('part-template/content', get_post_format());
            }
        }
        wp_reset_postdata();
    }

    die();
}

而已 !我认为有一种最好的方法可以做到这一点,但我仍然想向您展示我的代码。希望这可以帮助某人,如果您有任何意见、问题或想法,请随时在评论部分告诉我!

标签: phpjqueryajaxwordpresscustom-post-type

解决方案


只需将已经可见的 id 存储在数组中的某个位置,当请求加载更多时,发送该数组,然后将其包含到$args

$args = array (
...
'post__not_in' => array( 1, 2, 3),
...
);

推荐阅读