Customise WordPress search: post types, categories and order
The built-in search looks for the words in the title, the excerpt and the content of all public post types and sorts the results by relevance of the title, then by date. That is fine for a blog, but on a site with products, documentation and landing pages, visitors find pages they did not want and miss what they searched for. The main search query can be changed in pre_get_posts.
Problem: Search results contain pages and attachments that visitors should not land on, and the site needs a search inside one category.
Solution: In pre_get_posts, change the main search query: set post_type, exclude IDs, add a category from a form field. Searching in custom fields needs SQL filters and is covered in a separate post.
Only some post types
<?php
add_action( 'pre_get_posts', 'ha_search_post_types' );
function ha_search_post_types( $query ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() ) {
return;
}
$query->set( 'post_type', array( 'post', 'product', 'doc' ) );
}
The three checks matter: without them the change also hits the admin list screens, widgets and every other query on the search page.
A post type can also be left out of search entirely when it is registered: 'exclude_from_search' => true.
Excluding pages
<?php
add_action( 'pre_get_posts', function ( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
$exclude = array( 12, 34 ); // thank-you page, cart, legal pages...
$query->set( 'post__not_in', $exclude );
}
} );
Search in one category
Add a select field to the search form; WordPress passes its value as a query variable automatically:
<form role="search" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="search" name="s" value="<?php echo esc_attr( get_search_query() ); ?>">
<?php wp_dropdown_categories( array( 'show_option_all' => __( 'All topics', 'textdomain' ), 'name' => 'cat', 'selected' => get_query_var( 'cat' ) ) ); ?>
<button type="submit"><?php esc_html_e( 'Search', 'textdomain' ); ?></button>
</form>
cat is a public query variable, so /?s=cache&cat=7 searches only in category 7 without any PHP. For a custom taxonomy, use its query variable as the field name (for example doc_section if the taxonomy is public).
Order and number of results
<?php
add_action( 'pre_get_posts', function ( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
$query->set( 'posts_per_page', 20 );
// newest first instead of relevance:
// $query->set( 'orderby', 'date' );
}
} );
By default (since WordPress 3.7) search results are ordered by relevance: full phrase in the title first, then all words in the title, then matches in the excerpt and content. Change it only if dates really matter more, for example on a news site.
The default search does not look into custom fields. How to add them safely, with the right JOIN and without breaking the search condition, is shown in searching by custom fields without a plugin.
NOTE: SQL LIKE search does not understand word forms or typos and gets slow on big sites. When search is important for the site (a shop, a knowledge base), a dedicated search engine such as Elasticsearch (via ElasticPress), or a plugin with its own index such as Relevanssi or SearchWP, gives much better results.