-
Notifications
You must be signed in to change notification settings - Fork 805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Infinite Scroll: Prevent first page duplication, and fix main query detection #9112
Infinite Scroll: Prevent first page duplication, and fix main query detection #9112
Conversation
Previously, if you ran `is_main_query`, it would return false during the `pre_get_posts` hook because `is_main_query` compares the current query to the global. By setting to global first, these checks can be done correctly.
I have tested this with Storefront (and few other themes), but I'm still experiencing an issue:
|
@danieldudzic Thanks for testing. Please test with #9120 instead. Also make sure that none of the themes has any custom code to alter the IS query. You can test with woocommerce/storefront#788. |
^ +1 This patch alone won't make WC compatible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a small nitpick regarding the code, plus I seem to have problems when testing this PR. If you get a site with a big number of posts, you can observe for yourself:
- open a category view;
- scroll towards the second page;
- remember what posts you see;
- open the same link but in a new tab so that it has to load from the server;
- see that you don't get the same posts that were loaded in IS.
I can't put my finger on it, but I think that something is wrong with the offset, as though some posts are getting skipped because of posts per page number mismatch.
I'm happy to let you test on my site with tons of posts, just let me know if you need the creds. Here's a link to the site with IS and lots of posts with categories: https://dev71.lousy.site/category/cats/
$GLOBALS['wp_the_query'] = $GLOBALS['wp_query'] = new WP_Query( $query_args ); | ||
$GLOBALS['wp_the_query'] = $GLOBALS['wp_query'] = $infinite_scroll_query = new WP_Query(); | ||
|
||
$infinite_scroll_query->query( $query_args ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the PRs description, there's a pretty big change implied in this simple diff here. Do you think we should add a comment so that no one would have a desire to roll the thing back and avoid using an additional variable? :)
Or maybe we can even get rid of the variable, and just call the query
method on one of the globals after they are set?
After testing together with #9120 I can say that the problem is gone, well done, thank you! |
* Changelog 6.0: create base for changelog. * Add #8938 to changelog * Add #8962 to changelog * Add #8974 to changelog * Add #8975 to changelog * Add #8978 to changelog * Add #8867 to changelog * Add #8937 to changelog * Add #8961 to changelog * Add #8855 to changelog * Add #8944 to changelog * Add #8973 to changelog * Add #8977 to changelog * Add #8979 to changelog * Add #8980 to changelog * Add #8982 to changelog * Add #8983 to changelog * Add #8984 to changelog * Add #8986 to changelog * Add #9005 to changelog * Add #9010 to changelog * Add #9012 to changelog * Add #9021 to changelog * Add #9022 to changelog * Add #9056 to changelog * Add #9061 to changelog * Add #9079 to changelog * Add #9080 to changelog * Add #9088 to changelog * Add #9096 to changelog * Add #9097 to changelog * Add #9100 to changelog * Add #9107 to changelog * Add #8969 to changelog * Add #8993 to changelog * Add #9003 to changelog * Add #9031 to changelog * Add #8945 to changelog * Add #9052 to changelog * Add #9058 to changelog * Add #9066 to changelog * Add #9076 to changelog * Add #9053 to changelog * Add #9108 to changelog * Add #9135 to changelog * Add #9148 to changelog * Add #9125 to changelog * Add #9137 to changelog * Added testing instructions for 6.0. * Added IS testing instructions, huge props to @tiagonoronha. * Added #8498 to changelog. * Added #8954 to changelog. * Added #8985 to changelog. * add #9027 * add #9112 to changelog * add #9136 to changelog * add #9102 to changelog * add #9093 to changelog * add #9062 to changelog * add #9172 to changelog
I believe this fixes #1135 but we have slightly different reproduction steps in that we're simply running this on a custom post type archive. I found issues in the way pagination works which I'll do my best to explain below.
There are 2 main issues this PR aims to resolve;
Changes proposed in this Pull Request:
Globals
This PR adjusts the order in which the globals
wp_the_query
andwp_query
are set so thatis_main_query
is accurate when ran on thepre_get_posts
hook.Looking at the code where it set
wp_the_query
andwp_query
it looks like the intention here was to be the 'main query' but because the query is RAN before these globals are set, any functions hooking intopre_get_posts
will not see correct results.Before:
WP_Query
pre_get_posts
fires during the query.wp_the_query
andwp_query
have the original values. This meansis_main_query
will return false.After:
WP_Query
is constructed.wp_the_query
andwp_query
are set to this newWP_Query
object.pre_get_posts
fires during the query.wp_the_query
andwp_query
contain the current query object, sois_main_query
will return true.This fixes a conflict with WooCommerce which only adjusts the main query.
Pagination JavaScript
When viewing the first page and scrolling, IS loads page 1 again. The adjustment in the script I made makes it load page 1 plus the current page (offset).
So example, WooCommerce shop page at
local.wordpress.test/shop/
is showing page 1 already. When you scroll it loads page 1 + offset 1 = page 2.If you start at
local.wordpress.test/shop/page/2
it loads page 1 + offset 2 + page 3.Both the results are correct, and the address bar is updated accordingly.
I'm not sure how or if it actually worked before :)
Testing instructions:
So these are the testing instructions for our WooCommerce use case. @tiagonoronha May comment for general blog testing instructions but basically.
Before the patch you'd see random number of products, in random order, with missing items in the grid. Page 1 would also be duplicated.
Proposed changelog entry for your changes:
is_main_query
detection within infinite scroll's WP_Query object.