WordPress snippet: Search within a specific post type only

The following code snippet allows you to modify WordPress search engine to search only within a specific post type. Simply update the code with your post type name on line 4, then paste it into your theme functions.php file.

function SearchFilter($query) {
  if ($query->is_search) {
    // Insert the specific post type you want to search
    $query->set('post_type', 'feeds');
  }
  return $query;
}
 
// This filter will jump into the loop and arrange our results before they're returned
add_filter('pre_get_posts','SearchFilter');

Thanks to Hongkiat for the cool code snippet!

Leave a Reply

Your email address will not be published. Required fields are marked *