Sunday, October 28, 2012

Search within a specific post type only in Wordpress

Search within a specific post type only in Wordpress

In wordpress, when a user/visitor search website using WordPress built-in search function, wordpress always search through all post types. But sometimes we may need to be able to search only within a wordpress specific post type.The following code example will help to display that.


In the following code allows to modify WordPress search engine to search only within a specific post type. To work like that Just 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');

 

No comments:

Post a Comment

I just got my #domain @BigRock. Get upto 25% off with my personalized coupon link Get upto 25% off