Filter WordPress Search Results By Post Type

/ Published in: PHP
Save to your folder(s)

Copy this code and paste it in your HTML
  1. // Only search posts, and custom post types, excluding pages
  2. function search_posts_filter( $query ){
  3. if ($query->is_search){
  4. $query->set('post_type',array('post','custom_post1', 'custom_post2'));
  5. }
  6. return $query;
  7. }
  8. add_filter('pre_get_posts','search_posts_filter');

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.