首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >2使用与“orderby”=> rand相同的rand的自定义wp_query?

2使用与“orderby”=> rand相同的rand的自定义wp_query?
EN

Stack Overflow用户
提问于 2019-07-19 14:04:37
回答 1查看 737关注 0票数 0

在我的主页上,我显示了9篇文章,我有一个load更多的按钮(使用ajax),它调用9个其他帖子,等等。目前,我是按‘日期’订购,但我想随机订购。问题是,我的第一个wp_query orderby显示了9个随机帖子,而当我加载更多的post时,我的第二个wp_query命令也可以显示前9个post中相同的帖子。

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

正如一位社区成员所建议的,我尝试将可见Id存储在数组中,并使用post__not_in如下:

代码语言:javascript
复制
$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“获取所有post订单,例如:

代码语言:javascript
复制
$args = array(
        'post_type' => 'my_posts',
        'posts_per_page' => -1,
        'orderby' => 'rand'
    );

然后在我的循环中显示第9篇文章,在9篇文章之后我得到了所有其他帖子的ID

代码语言:javascript
复制
<?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 元素列表转换为string list

代码语言:javascript
复制
$index = 0;
$idsString = array();

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

这里是我的“加载更多”按钮,我输入了“数据-ID”我的ID列表。

代码语言:javascript
复制
<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函数看起来像:

代码语言:javascript
复制
$(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);

            }
        }
    });
});

我的$_POST['page']包含了在每次加载更多调用时增加的一些页面,稍后我将解释使用它的原因,我的$_POST['foo']在我的第一个查询循环中包含我的ID创建列表。

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

代码语言:javascript
复制
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();
}

就这样!我认为有一个最好的方法来做到这一点,但我仍然想向您展示我的代码。希望这能帮助别人,如果你有评论,问题,或想法,不要犹豫告诉我在评论部分!

EN

回答 1

Stack Overflow用户

发布于 2019-07-19 14:23:26

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

代码语言:javascript
复制
$args = array (
...
'post__not_in' => array( 1, 2, 3),
...
);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57114177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档