Jetpack: Different orderby causes double load in Infinite Scroll

Created on 26 Sep 2014  路  14Comments  路  Source: Automattic/jetpack

Hi,

When loop's orderby is different then "modified" or "date", get_last_post_date and get_query_sort_field functions returns false and that causes double load first posts.

(Edit: talking about Infinite Scroll)

Infinite Scroll [Type] Bug

Most helpful comment

Using @davidmosterd solution worked for me. But since I was only using orderby for one post_type I was having trouble with the infinite_scroll_query_args returning default results.

Thanks to @pavelevap I tried a higher priority and it worked.

Final Code:

function hm_fix_people_infinite_scroll_paging( $args ) {
    if ( 'people' === $args['post_type'] ) {
        $args['paged']++;
    }
    return $args;
}
add_filter( 'infinite_scroll_query_args', 'hm_fix_people_infinite_scroll_paging', 100 );

All 14 comments

I tried this:

add_filter( 'infinite_scroll_query_args', 'firmasite_infinite_scroll_orderby_fix');
function firmasite_infinite_scroll_orderby_fix($query_args) {
    switch ($_REQUEST['query_args']['orderby']){
        case '':
        case 'modified':
        case 'date':
            break;
        default:
            $query_args['offset'] = $query_args['posts_per_page'];
            break;
    }
    return $query_args;
}

It works for first load but then it corrupts loading so i need a different solution. Any tips?

@lancewillett -- is Infinite Scroll still @ethitter's baby, or is it yours now?

@georgestephanis Tagging with the tdiv label is better than pinging individuals. As teams and responsibilities change, the appropriate person can triage the labeled issues.

@georgestephanis We don't own anything anymore鈥攖div works on it as a whole.

Groovy, thanks!

I too have this problem. I sorted on a meta query with meta_value_num. The sorting clause gave me the same issue: first page was duplicated. I am using this work-around for now:

/**
 * Fix the paged parameter
 *
 * @param array $args
 * @return array
 */
function my_infinite_scroll_fix_paged( $args ) {
    $args['paged']++;

    return $args;
}
add_filter( 'infinite_scroll_query_args', 'my_infinite_scroll_fix_paged' );

I have no idea if this would work in other scenario's as well, but I thought I could share it here at lease.

I am suscribing to this bug.

Managed to make it work, still testing though.
I want to sort posts by meta_value_num, the meta_key being _rating_general, and DESC order, here is the filter:

function jetpack_infinite_scroll_query_args( $args ) {
    $args['orderby'] = 'meta_value_num';
    $args['meta_key'] = '_rating_general';
    $args['order'] = 'DESC';
    $args['offset'] = $args['posts_per_page'] * $args['paged'];
    return $args;
}
add_filter( 'infinite_scroll_query_args', 'jetpack_infinite_scroll_query_args' );

Here is the complete gist:
https://gist.github.com/nicomollet/5a18896f56701e44b285

Same problem. Also when I tried to check for example $args['post_type'] inside infinite_scroll_query_args filter, I received bad (default) results. I had to check $_REQUEST['query_args']['post_type'] to get relevant values. And that is strange because $args is passed into this filter and should contain relevant information.

Sorry, I did not realize, that I have to call add_filter() for infinite_scroll_query_args with higher priority, because there is inject_query_args() hooked at default 10.

Using @davidmosterd solution worked for me. But since I was only using orderby for one post_type I was having trouble with the infinite_scroll_query_args returning default results.

Thanks to @pavelevap I tried a higher priority and it worked.

Final Code:

function hm_fix_people_infinite_scroll_paging( $args ) {
    if ( 'people' === $args['post_type'] ) {
        $args['paged']++;
    }
    return $args;
}
add_filter( 'infinite_scroll_query_args', 'hm_fix_people_infinite_scroll_paging', 100 );

Also reported here: #2679697-t

I also posted a fix in https://wordpress.org/support/topic/is-there-any-post-ordering-plugin-that-works-with-infinite-scroll
and hope someone who needs help can find the temp fix easily ( also, try bringing out more discussion on this)

I am using jetpack version 4.3.1. My fix is in the below, so far so good.

add_filter( 'infinite_scroll_query_args', 'jetpack_infinite_scroll_query_args' );

function jetpack_infinite_scroll_query_args( $args ) {

    /* Customize the ordering */
    $args['orderby'] = 'meta_value_num';
    $args['meta_key'] = 'YOUR_META_KEY';
    $args['order'] = 'DESC';

    try{
        /* Get the start page, if the request is from 1st page, it will be 0,  and then no need to do decrement. */
        $startPage = intval($_POST['query_args']['paged']);
        if($startPage != 0) $startPage--;

        /* Adding $args is to avoid the people making infinite scrolling request in
         non 1st page */
        $currentPage = ($args['paged'] + $startPage);

    }catch(Exception $x){
        /* If error exists, I just use back the paged argument */
        $currentPage = $args['paged'];
    }

    /* Using offset is used to override the paged args which will be used in WP_Query */
    $args['offset'] = $args['posts_per_page'] * $currentPage;

    return $args;
}

If you will use infinite scroll for custom post type with meta_key ordering, you have to change the query params in 'pre_get_posts' hook too to make the post display consistent.

Was this page helpful?
0 / 5 - 0 ratings