jetpack icon indicating copy to clipboard operation
jetpack copied to clipboard

Infinite scroll returns nothing if post-type is set

Open Farsad opened this issue 6 years ago • 2 comments

I'm going to limit infinite scroll to a specific post-types on the search and archive pages. It doesn't work on the archive page of custom post types and also on the search pages if I set the post-type.

Steps to reproduce the issue

1- Install a new _s theme. 2- Activate jetpack infinite scroll. 3- Add search filter like:

function search_filter($query) {
    if ($query->is_search && !is_admin() ) {
        $query->set('post_type', 'post');
    }
    return $query;
}
add_filter('pre_get_posts','search_filter');

into /themes/test/inc/template-functions.php.

4- Search for posts and scroll for more results

What I expected

It should be load only default WordPress post.

What happened instead

Infinite scroll doesn't work. The ajax call return {“type”:”empty”} instead of the posts.

To help you debug it I just added a print_r($query_args); on infinity.php at line 1338 just before $infinite_scroll_query->query( $query_args );.

here is a diff between both results: https://www.diffchecker.com/4tsRv71x

Please let me know if you need any more information.

Farsad avatar Oct 15 '20 08:10 Farsad

There are hooks you can use to customize the parameters of all Infinite Scroll queries. I'd recommend checking infinite_scroll_query_args (reference) as well as the other hooks available in the Infinite Scroll feature. Those hooks are available in the codex at the links above, but you can also see them in the codebase right here: https://github.com/Automattic/jetpack/blob/ad8ec79d5b67572e4b6d10ac8e08a542186c4032/modules/infinite-scroll/infinity.php

jeherve avatar Oct 15 '20 10:10 jeherve

Hi @jeherve ,

Inside infinite_scroll_query_args, is it possible to pass on $_GET?

As an example, this is what I am trying to do:

function kh_infinite_scroll_query_args( $args ) {
	
		$args['tax_query'] = array(
			array (
				'taxonomy' => 'tadxp-service',
				'field' => 'slug',
				'terms' => $_GET['_services'],
			)
		);

	return $args;
}
add_action( 'infinite_scroll_query_args', 'kh_infinite_scroll_query_args', 11 );

amigoow avatar Jan 13 '22 20:01 amigoow