Php 2个自定义wp_查询,使用与';订购人'=&燃气轮机';兰德';?

Php 2个自定义wp_查询,使用与';订购人'=&燃气轮机';兰德';?,php,jquery,ajax,wordpress,custom-post-type,Php,Jquery,Ajax,Wordpress,Custom Post Type,在我的主页上,我展示了9篇文章,我有一个加载更多按钮(使用ajax),它调用了9篇其他文章,以此类推。目前我按“日期”订购,但我想随机订购。问题是,我的第一个wp_查询orderby rand显示9个随机帖子,当我加载更多帖子时,我的第二个wp_查询orderby random也可以显示前9个帖子中的相同帖子 如何使用相同的随机数将2个wp_查询随机化?还是有其他方法可以做到这一点 正如一位社区成员已经建议的那样,我尝试将可视Id存储在一个数组中,并使用post\u not\u in类似于:

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

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


正如一位社区成员已经建议的那样,我尝试将可视Id存储在一个数组中,并使用
post\u not\u 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_查询时,我得到9个随机帖子减去可能的9个第一个帖子,因此我可以得到少于9个帖子,当到达最后一页时查询停止。我将有大约28篇文章,而不是32篇,因为查询会在第4页停止,直到所有的文章都显示出来

提前感谢您的回答

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

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();
}
我将第一个查询更改为按“兰德”获取所有邮政订单,如:

$args = array(
        'post_type' => 'my_posts',
        'posts_per_page' => -1,
        'orderby' => 'rand'
    );
$(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);

            }
        }
    });
});
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();
}
然后在我的循环中显示前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;
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();
}
这是我的“加载更多”按钮,我在“数据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>
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();
}
我的
$\u POST['page']
包含许多页面,这些页面在每次Load More调用时都会增加,我稍后将解释为什么要使用它,我的
$\u 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();
}
function home\u load\u more()
{
$ids=($_POST['foo']);
$paged=$_POST[“page”];
$postIn=array();
//我只想在每次通话中显示9条帖子
对于($i=0;$i<9;$i++){
/*这是我用来计算身份证的奇怪算法
-第一次呼叫时从0到8
-第二次通话从9点到17点
-第三次通话时从18点到26点
等等
这就是为什么我使用$paged来获取
每次调用时递增一的值*/
//如果列表中没有其他ID,则中断
如果($ids[($paged-1)*9+$i]==null){
打破
}
//$postIn包含9个posts id
数组推送($postIn,$ids[($paged-1)*9+i]);
}
$query=新的WP\U查询(数组(
“帖子类型”=>“我的帖子”,
'orderby'=>'post\uu in',//这里我给出了我的9个ID列表
'post_uuin'=>$postIn
));
如果($postIn[0]==null){
//“加载更多”按钮不会显示任何帖子
wp_reset_postdata();
}否则{
如果($query->have_posts()){
而($query->have_posts()){
$query->the_post();
get_template_part('part-template/content',get_post_format());
}
}
wp_reset_postdata();
}
模具();
}

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

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

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

我的wp_查询位于两个不同的文件中,如何全局使用id为的变量?我已经测试过这个解决方案,但无法在第二个文件中检索id,我在functions.php中使用了一个全局变量,但在第二个文件中总是返回“null”。您不需要将其设置为全局值,可以将id添加为$\u GET。当你可以在
functions.php
中做任何事情,甚至在一个函数上创建单独的函数时,为什么要做不同的文件呢。