By default, WordPress search will only show your blog posts. You can also add the other post types in search to expand it.
Instructions: Add the following code to the functions.php file of your WordPress theme to include all post types in your search.
<?php
function pippin_include_post_types_in_search($query) {
if(is_search() && is_main_query()) {
$post_types = get_post_types(array('public' => true, 'exclude_from_search' => false), 'objects');
$searchable_types = array();
if($post_types) {
foreach( $post_types as $type) {
$searchable_types[] = $type->name;
}
}
$query->set('post_type', $searchable_types);
}
return $query;
}
add_action('pre_get_posts', 'pippin_include_post_types_in_search');
?>
You may also enjoy disabling public search in WordPress and filtering search by users in WordPress.
Comments Leave a Reply