最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - WP_Query offset returning duplicate posts from previous loops with AJAX

programmeradmin0浏览0评论

I have run into the following issue using WP_Query:
I'm running a large query and have, therefore, split it up in batches using AJAX with an increasing offset in each loop. The issue I'm facing is that the entire process returns a few duplicates (posts from a previous loop appearing in the following one) - not always the exact same ones either.
The query looks like this:
    $offset = $_POST['offset'];
    $posts_per_page = 100;
    $args = array(
        'post_type'      => array('player'),
        'post_status'    => 'publish',
        'posts_per_page' => $posts_per_page,
        'offset'         => $offset,
        'order'          => 'DESC',
        'ignore_sticky_posts' => 1,
        //'fields'         => 'ids',
        'tax_query' => array(
            array(
                'taxonomy' => 'team',
                'field'    => 'term_id',
                'terms'    => [23,46,54,12,45],
            ),
        )
    );
    $posts = new WP_Query( $args );

The above is run repeatedly using AJAX, with the only thing changing being the $offset, which increases each time by 100 (equal to the previous offset plus the value of $posts_per_page) until all posts get queried.
Sample of post IDs returned:
1st loop: [...,3606,3587,3588,3569,...]
2nd loop: [3585,3586,3587,3588,3556,...]
3rd loop: [...,1594,2524,3026]
4th loop: [2524,3026,3009,...]
etc.
Any help with this would be greatly appreciated.

Similar issues I've found online but which don't seem to offer a solution that's working for me:
/
WP_Query offset is returning post from prevois loop

发布评论

评论列表(0)

  1. 暂无评论