Automattic / Automattic/jetpack

Infinite Scroll thinks archive page is the homepage on my site

Open
#10,013 3 comments 0 reactions 0 assignees View on GitHub
[Feature] Infinite Scroll [Pri] Low [Status] Auto-allocated [Status] Stale Bug
Dominant language
PHP
Stars
1.8k
Forks
898
Avg merge
1d 18h
Merged PRs (30d)
774

Description

#### Steps to reproduce the issue

It appears that the changes that were made in tag 6.0 on lines 1263 to 1265 in modules/infinte-scroll/infinite-scroll.php broke one of our sites. The particular bug is that my conditional in a pre-get-posts hook that I have no longer works when we call $wp_query->query() after we make the object for some reason.

This is my condition in the pre-get-posts:
``` php
is_main_query() && is_home() ) {

// $query->get('page_id') == get_option('page_on_front') // check for homepage if it isnt the posts page
$posttypes = array( 'post','news','projects' );

$wp_query->set( 'post_type' , $posttypes );
$wp_query->set( 'meta_query', array(
array(
'key' => 'featured',
'value' => '1',
'compare' => '!=',
),
));
} elseif ( ! is_admin() && $wp_query->is_main_query() && ( array_key_exists( 'post_type', $wp_query->query ) && 'blog' == $wp_query->query['post_type'] ) ) {
//Overide the faux blog page to show the actual blog.
$posttypes = array( 'post' );
$wp_query->set( 'post_type' , $posttypes );
$wp_query->set( 'meta_query', array(
array(
'key' => 'featured',
'value' => '1',
'compare' => '!=',
),
));
// for the archive pages
} // and more if statements
```

So when we are on the archive page for the post type "blog" we end up going into the first condition instead of the 2nd like I would expect. That is, the `array_key_exists( 'post_type', $wp_query->query )` is false, and `is_home()` is true even though I am on the "blog" custom post type archive page.

If we revert Jetpack to how it was in 5.9 everything works fine again.

So as far as steps go to reproduce

1. Have a custom post type archive page with a crazy custom query on it using pre_get_posts
2. If you examine the $wp_query object from those lines you will see that the post_types variable gets set properly in the `query` variable but not in the `query_vars` variable

#### What I expected

I expected the query that jetpack infinite scroll uses to match the one that loads on the page

#### What happened instead

- when jetpack calls the ajax function the `is_home()` call here in pre-get-posts is set to true even though we are on an archive. This causes us to pull the 3 post types defined in the code above
- The same thing as the above bullet but phrased a different way: we are on the blog archive but the post type is never being set to `blog` so we never go into the second if statement. Because wordpress doesn't think we are on an archive it assumes we are on the homepage.

#### Conclusions

So this issue is mostly to say "pretty please can you change it back?", from the change log the reason for changing these lines in Jetpack is:

```
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.
```

but as you can see here we are just calling `$wp_query->is_main_query()` from the global anyway. Of course, there is probably someone else out there whose site broke because it was the other way. Just let me know if you will fix this issue in jetpack or if I have to re-write the way that we handle the infinite scroll on our site

Contributor guide

Open the contributing guide

Research direction

Inspect modules/infinte-scroll/infinite-scroll.php around lines 1263-1265 and the change introduced in Jetpack 6.0. Reproduce a custom post type archive using pre_get_posts, then compare the $wp_query query and query_vars values during the Infinite Scroll AJAX query. Done means the AJAX query preserves the archive context and does not treat the blog archive as the homepage.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, wordpress
Domain
backend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.