Automattic / jetpack

Security, performance, marketing, and design tools — Jetpack is made by WordPress experts to make WP sites safer and faster, and help you grow your traffic.
https://jetpack.com/
Other
1.59k stars 799 forks source link

Infinite Scroll thinks archive page is the homepage on my site #10013

Open kipraske opened 6 years ago

kipraske commented 6 years ago

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
//Overide the homepage to grab Posts, News and Projects
    if ( ! is_admin() && $wp_query->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

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

kipraske commented 6 years ago

Now that I have had a weekend to think about it a bit. Maybe you all can put a hook in there to determine which way we want to run the query. That way the folks that need is_main_query() can have everything keep working the way that it used to, and I can use the old way for my old theme here. That way I can avoid forking Jetpack and everyone wins.

wpalchemist commented 5 years ago

I'm having the same problem. On the homepage, I use pre_get_posts to prevent some posts from showing on the homepage, but that filter runs on infinite scroll queries on the rest of the site too.

My code:

function filter_front_page_query( $query ) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
        $do_not_duplicate = get_do_not_duplicate();
        $query->set( 'post__not_in', $do_not_duplicate );
    }
}
add_action( 'pre_get_posts', 'filter_front_page_query' );

Even if I change the priority, those posts get excluded from all infinite scroll queries.

stale[bot] commented 5 years ago

This issue has been marked as stale. This happened because:

No further action is needed. But it's worth checking if this ticket has clear reproduction steps and it is still reproducible. Feel free to close this issue if you think it's not valid anymore — if you do, please add a brief explanation.