SearchWP Documentation

View the installation guide, browse the Knowledge Base, find out about SearchWP’s many hooks

Limit results to Post Type (e.g. Posts, Pages)

By default SearchWP will return results according to the configuration of the Engine used to perform the search. There are times where you’d like to build a form that allows visitors to limit the results to any number of Sources added to the Engine. Once the form is built, we can use a Mod to limit the results returned by the Engine to those specified by the form.

The basic implementation looks like this:

All hooks should be added to your custom SearchWP Customizations Plugin.

<?php
// Limit SearchWP results to Posts and Pages.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
$mod = new \SearchWP\Mod();
$mod->set_where( [ [
'column' => 'source',
'value' => [
\SearchWP\Utils::get_post_type_source_name( 'post' ),
\SearchWP\Utils::get_post_type_source_name( 'page' )
],
'compare' => 'IN',
] ] );
$mods[] = $mod;
return $mods;
}, 10, 2 );

Note: This snippet illustrates limiting results to Posts and Pages, without taking into consideration the form input.

We can integrate this Mod with form input (assuming the $_GET variable has a name of sources) like so:

All hooks should be added to your custom SearchWP Customizations Plugin.

<?php
// Limit SearchWP results to form field value.
// `sources` is a GET array of post type names.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
if ( empty( $_GET['sources'] ) ) {
return $mods;
}
$mod = new \SearchWP\Mod();
$mod->set_where( [ [
'column' => 'source',
'value' => array_map( function( $source ) {
return \SearchWP\Utils::get_post_type_source_name( $source );
}, $_GET['sources'] ),
'compare' => 'IN',
] ] );
$mods[] = $mod;
return $mods;
}, 10, 2 );

Note: This snippet assumes that your form variable has been set up as checkboxes that build an array of post type names, and that the post type names match those that have been added to the Engine in use, e.g.

<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<label>
<span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span>
<input type="search" class="search-field"
placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>"
value="<?php echo get_search_query() ?>" name="s"
title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
</label>
<p>
<input type="checkbox" class="search-field" id="source-post"
value="post" name="sources[]" />
<label for="source-post">Posts</label>
</p>
<p>
<input type="checkbox" class="search-field" id="source-page"
value="post" name="sources[]" />
<label for="source-page">Pages</label>
</p>
<input type="submit" class="search-submit"
value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" />
</form>
view raw tmp.html hosted with ❤ by GitHub

(Specifically lines 9-18)

Create a Better WordPress Search Experience Today

Never lose visitors to unhelpful search results again. SearchWP makes creating your own smart WordPress search fast and easy.

Get SearchWP Now
Multiple Search Engines Icon