Jetpack Infinite Scroll for search results

Jetpack has the infinite scroll functionality activated by default for archive and blog views. However, it’s turned off for search results views.

Overview

Infinite Scroll includes a filter to indicate if it’s supported or not, and we can use a conditional to alter the returned value. The filter is infinite_scroll_archive_supported and it receives a boolean stating the supported state for the current view and the infinite scroll initialization settings as parameters.

Filtering the current view support

We’ll write a condition similar to the one found in the filter but adding is_search so it kicks in a search results view. You can paste the following in your theme’s functions.php or similar:

/**
 * Enables Jetpack's Infinite Scroll in search pages, disables it in product archives
 * @return bool
 */
function tr_theme_jetpack_infinite_scroll_supported() {
	return current_theme_supports( 'infinite-scroll' ) && ( is_home() || is_archive() || is_search() );
}
add_filter( 'infinite_scroll_archive_supported', 'tr_theme_jetpack_infinite_scroll_supported' );

Disabling Infinite Scroll on custom post type archives

Perhaps you’ve a certain custom post type archive, like WooCommerce products and you want to disable Jetpack’s Infinite Scroll for WooCommerce product shop and archives. This is what you would use then:

/**
 * Enables Jetpack's Infinite Scroll in search pages, disables it in product archives
 * @return bool
 */
function tr_theme_jetpack_infinite_scroll_supported() {
	return current_theme_supports( 'infinite-scroll' ) && ( is_home() || is_archive() || is_search() ) && ! is_post_type_archive( 'product' );
}
add_filter( 'infinite_scroll_archive_supported', 'tr_theme_jetpack_infinite_scroll_supported' );

We’re not using a function like is_woocommerce because if you ever disable the WooCommerce plugin, the site will go blank since the is_woocommerce function will no longer be defined and it will throw a fatal error.

Leave a Reply