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 797 forks source link

Infinite Scroll: Pages with custom query using category__in or tag__in don't paginate properly #4087

Open renatoalbano opened 8 years ago

renatoalbano commented 8 years ago

What I expected

When I set a custom query using pre_get_posts action with multiple categories or tags, for example:

function my_custom_query( $query ) {
  if( $query->is_tag() ) {
    $query->set( 'category__in', array( 42, 43, 44 ) );
  }
}
add_action( 'pre_get_posts', 'my_custom_query' );

The posts that was rendered in page was correct filtered by categories 42, 43, 44.

When I click or scroll to load more posts through infinite scroll I expect the posts that were added to page are using 42, 43, 44 categories.

What happened instead

When I click or scroll through infinite scroll, the posts that were added to page is only from category 42, the first category which was assigned at pre_get_posts filter.

Steps to reproduce the issue

If you inspect javascript global variable infiniteScroll at page, it has the category__in array with correct ids assigned at pre_get_posts. But the key category_name and cat in infiniteScroll.settings.query_args has only the first category slug and id. Apparently this was correct, because it was used for backward compatibility, as was being explained in this core ticket.

But I think infinite scroll query was only considering the category at cat or category_name at query_vars and ignoring category__in.

I did a workaround using infinite_scroll_query_args action, but i think infinite scroll should work as is in wordpress rendered page.

I don't tested with tag__in filter, but I think that has the same issue.

I was using:

jeherve commented 8 years ago

I did a workaround using infinite_scroll_query_args action, but i think infinite scroll should work as is in wordpress rendered page.

Yes, I believe that's the expected way of doing things to get it to work today.

How do you think we should go about this instead?

renatoalbano commented 8 years ago

@jeherve: Yes, I believe that's the expected way of doing things to get it to work today. How do you think we should go about this instead?

I think as the infinite scroll is already receiving query_vars in ajax request it should consider what was passed. In this case, keys ending with __and,__in, __not_in.

Using infinite_scroll_query_args do redefining query_args of infinite scroll query I need to duplicate my business rules in pre_get_posts and in infinite_scroll_query_args actions, besides having to add some extra settings to pass to infinite_scroll_query_args action what page is, if is is_tag() or is_category() and etc.

I am replying as plugin user, idk infinite scroll code base and WP_Query very well to suggest more technical answer.

jeherve commented 8 years ago

cc @ethitter Happy to have your opinion on this, since you worked on infinite_scroll_query_args.

daco commented 7 years ago

I'm encountering probably the same issue when looking at a category archive with multiple categories, eg /category/catA+catB+catC+etc/ Only catA is considered upon loading more posts via Infinite scroll. Anyone any thoughts?

daco commented 7 years ago

Okay. For me this fixes the problem: Adding this in function get_query_vars() in infinity.php

if (self::wp_query()->query['category_name']) {
     $query_vars['category_name'] = self::wp_query()->query['category_name'];
}

But it feels quite hacky. This would be no candidate for PR would it?

stale[bot] commented 6 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.

daco commented 4 years ago

This issue still exists!

daco commented 4 years ago

I "fixed" this issue with this snipped:

function infinite_scroll_query_object($query) { $query->set("category_name", $query->query["category_name"]); return $query; } add_filter('infinite_scroll_query_object', 'infinite_scroll_query_object', 9999);

jacobjavits commented 2 years ago

Having a sort of similar issue which I posted in the WP support forum and was directed here (https://wordpress.org/support/topic/infinite-scroll-does-not-respect-tag-query-var/#post-15269919)

I set up a blog post filter in my theme so that you can filter posts by category or tag. I update the infiniteScroll.settings.query_args values via Javascript when the form is filled out. This works fine for infiniteScroll.settings.query_args.category_name but not for infiniteScroll.settings.query_args.tag (or any other permutation of the tag query vars, e.g. tag_id, tag__in, etc).

I’ve confirmed that the XHR request is sending the correct query vars and is being received by Jetpack, but it seems to ignore tags entirely.