facetwp_search_query_args
Overview
With this hook, you can change the search query arguments used by Search facets, before the query is executed.
This hook only works for Search facets that have their search engine set to “WP Default“.
Parameters
- $search_args | array | An associative array of query arguments:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
// Default WP search arguments $search_args = [ 's' => $selected_values, // the search string 'posts_per_page' => 200, // Default. Use -1 for all posts 'fields' => 'ids', ];
- $params | array | An associative array of facet settings and the selected values (the search keyword string):
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
$params = [ 'facet' => [ 'name' => 'my_search_facet', 'label' => 'my search facet', 'type' => 'search', 'search_engine' => '', // empty for "WP Default" 'placeholder' => 'Type keywords', 'auto_refresh' => 'yes', 'operator' => 'or', 'selected_values' => 'used keyword', 'enable_relevance' => 'yes', ], 'selected_values' => 'used keyword' ];
Usage examples
Customize the post limit for the WP Default engine
When using the “WP Default” search engine, a Search facet will return a maximum of 200 results. To remove or customize this limit, add the following code to your (child) theme’s functions.php.
If you set the posts_per_page
to -1
, the number of results will not be limited, and all results will be retrieved. Be aware that doing this may harm performance if your listing template query has a high number of posts.
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_filter( 'facetwp_search_query_args', function( $search_args, $params ) { $search_args['posts_per_page'] = -1; // Default: 200. Set to -1 to show all search results. return $search_args; }, 10, 2 );
To customize the post limit when using a SearchWP engine, see this section, and for Relevanssi see this section.
Include draft posts
The following example shows how to force draft posts to appear in the search results by adding them to the post_status
query argument:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_filter( 'facetwp_search_query_args', function( $search_args, $params ) { $search_args['post_status'] = [ 'publish', 'draft' ]; return $search_args; }, 10, 2 );
Exclude posts from search results
Using the post__not_in
query argument, you can prevent certain posts from showing up in the search results:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_filter( 'facetwp_search_query_args', function( $search_args, $params ) { $search_args['post__not_in'] = [ 893,894 ]; // The post ID(s) to exclude from the search results return $search_args; }, 10, 2 );