Skip to content

Snippet: Limit the Ajax Search Post Types

If you're using the new search modal that displays results via AJAX, the request is sent to WordPress using the default query, which returns any posts matching the search term and having a 'publish' status. When registering custom post types, you can set the "exclude_from_search" parameter to false to ensure they're included in search results. However, there's a known bug in WordPress that causes custom post types to be excluded from custom queries, even if "exclude_from_search" is set to false.

To control which post types are displayed in the search results, you'll need to add some custom code. This snippet hooks into the AJAX live search process, modifying the query arguments to allow you to specify which post types should appear in the search results.

// Filters the live search query args.
function myprefix_filter_ajax_search_args( $args ) {
	$args['post_type'] = [ 'page', 'post' ];
	return $args;
}
add_filter( 'totaltheme/search/ajax/query_args', 'myprefix_filter_ajax_search_args' );
All PHP snippets should be added via child theme's functions.php file or via a plugin.
We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top