Jetpack: Infinite scroll in Jetpack 6.0 not returning expected results

Created on 17 Apr 2018  路  6Comments  路  Source: Automattic/jetpack

Steps to reproduce the issue

  1. Upgrade Jetpack 5.9 to 6.0
  2. Load more posts in page with a button
  3. Filter the results with these functions:

    • First alter the query to only return posts from the "blog-post" category

      function filter_jetpack_infinite_scroll_excludes( $query ) { if ( $query->is_home() && $query->is_main_query() && !is_admin() ) { $query->set( 'category_name', 'blog-post' ); $query->set( 'offset', '2' ); wp_reset_postdata(); //Reset the postdata } } add_action( 'pre_get_posts', __NAMESPACE__ . '\\filter_jetpack_infinite_scroll_excludes', 100 );

  • Next update the arguments used by infinite scroll
    function jetpack_infinite_scroll_query_args( $args ) { $args['category_name'] = 'blog-post'; $args['offset'] = $args['posts_per_page'] * ($args['paged'] - 1); return $args; } add_filter('infinite_scroll_query_args', __NAMESPACE__ . '\\jetpack_infinite_scroll_query_args', 100 );
  1. Refresh the blog index page

What I expected

The display of the 4 latest posts (4 is set in the WP reading options) offset by 2 posts (the latest 2 posts get a special treatment elsewhere). A button that when clicked will load the next 4 posts in order.

What happened instead

The display of the latest 4 latest posts, offset by 2 posts.
First click of the "load more button" loads the next 4 posts but is offset by 2.
Subsequent clicks on "load more button" load the same 4 posts above, over and over again.

There are two problems here:

  1. Upon the first click of the "load more button" the returned results should NOT be offset by 2, only the initial render of the query should be offset by 2.
  2. The subsequent clicks of the "load more button" loading the same results over and over again.

Why I'm so confused is that the above functions work as expected in Jetpack 5.9 but upon upgrade to 6.0 they break and result in the above.

Infinite Scroll [Pri] Normal [Status] Needs Author Reply [Type] Bug

Most helpful comment

@mikejolley Absolutely beautiful, you are a scholar and a gentleman, that did it... albeit some minor adjustments for my needs. My final function looks like so:

function filter_jetpack_infinite_scroll_excludes( $query ) {
  if ( ! $query->is_main_query() ) {
    return;
  }
  if ( $query->is_home() && $query->is_main_query() && !is_admin() ) {
    if ( $query->is_paged ) {
      $query->set( 'category_name', 'blog-post' );
      $query->set( 'offset', 2 + ( $query->query_vars['paged'] - 1 ) * $query->query_vars['posts_per_page'] );
    } else {
      $query->set( 'category_name', 'blog-post' );
      $query->set( 'offset', 2 );
    }
  }
}
add_action( 'pre_get_posts', 'filter_jetpack_infinite_scroll_excludes', 100 );

This now works as expected.

All 6 comments

Could you try to apply the patch in #9256, and let us know if that fixes the problem for you?

Thank you!

OK tried the patch and this is what happens now:

  • Initial load of the page is displayed correctly. The offset by 2 posts works as intended and the subsequent 4 posts are displayed

  • However, pressing the "load more button" now loads the same 4 posts, ad infinitum.

@rjdusk as explained by @mikejolley in this comment, using the offset parameter in a query causes it to ignore the paged parameter which in turn breaks the new logic for the Infinite Scroll module.

My suggestion would be to exclude the posts from the query with post__not_in instead of using the offset parameter.

You would, however, need to run a small query before to grab the ids of the posts that you want to exclude.

Also, the code you have above to filter infinite_scroll_query_args shouldn't be necessary as of #9256. The parameters of the query are carried over when doing the AJAX request.

@tiagonoronha @jeherve @rjdusk Unlike the old version of Jetpack, is main query detection has been fixed. So you no longer need to set offset via Infinite scroll. A snippet like this will work going forward:

function filter_jetpack_infinite_scroll_excludes( $query ) {
    if ( ! $query->is_main_query() ) {
        return;
    }
    if ( $query->is_paged ) {
        $query->set( 'offset', 2 + ( $query->query_vars['paged'] - 1 ) * $query->query_vars['posts_per_page'] );
    } else {
        $query->set( 'offset', 2 );
    }
}
add_action( 'pre_get_posts', 'filter_jetpack_infinite_scroll_excludes', 100 );

@mikejolley Absolutely beautiful, you are a scholar and a gentleman, that did it... albeit some minor adjustments for my needs. My final function looks like so:

function filter_jetpack_infinite_scroll_excludes( $query ) {
  if ( ! $query->is_main_query() ) {
    return;
  }
  if ( $query->is_home() && $query->is_main_query() && !is_admin() ) {
    if ( $query->is_paged ) {
      $query->set( 'category_name', 'blog-post' );
      $query->set( 'offset', 2 + ( $query->query_vars['paged'] - 1 ) * $query->query_vars['posts_per_page'] );
    } else {
      $query->set( 'category_name', 'blog-post' );
      $query->set( 'offset', 2 );
    }
  }
}
add_action( 'pre_get_posts', 'filter_jetpack_infinite_scroll_excludes', 100 );

This now works as expected.

Excellent! Closing this issue now.

Was this page helpful?
0 / 5 - 0 ratings