Search only post type in wordpress

By default wordpress search provides a searching function on whole site whether it is pages or post type. In order to make the search only post type in wordpress, below is the code you need to copy and paste it in to your function.php file.

function filter_search($query) {
if ($query->is_search) {
$query->set('post_type', array('post','page'));
};
return $query;
};
add_filter('pre_get_posts', 'filter_search');
Notice the line that says
$query->set('post_type',array('post','page'));
We can filter the search results by changing the values in the array variable. Right now it is set to display posts and pages but you can modify it to display anything we required.

10 comments:

  1. Chatbots play a significant role in nurturing leads and optimizing sales by analyzing customer data. It can track purchasing patterns and monitor their browsing activity thus can be ideal for initiating customer interaction. With this customer knows brand is present whenever needed Chatbot Development Company

    ReplyDelete
  2. Have you ever wondered how much it would cost to develop an app for iOS or Android? This handy app cost calculator will help you. Get the free mobile app development cost in a few clicks. Just tick the desired features and get an estimate instantly. Use our smart App cost calculator tool.

    ReplyDelete

  3. We are a top real estate software development company provides custom real estate software solutions to seamlessly manage workflows and handle structured and unstructured data to nurture new business opportunities. We enable digital innovations to invade all the spheres of modern real estate and reshape it to match the new reality.

    ReplyDelete
  4. Chatbots play a significant role in nurturing leads and optimizing sales by analyzing customer data. It can track purchasing patterns and monitor their browsing activity thus can be ideal for initiating customer interaction. With this customer knows brand is present whenever needed Chatbot Development Company

    ReplyDelete