How to include specific custom post types in WordPress search results

How to include specific custom post types in WordPress search results

Custom post types are not included in the WordPress search results by default – adding this snippet to you functions.php creates a function that allows specific custom post types to be queried and searched by WordPress.

Instructions
Add this to your functions.php file

add_filter( 'pre_get_posts', 'custom_post_type_search' );
function custom_post_type_search( $query ) {
     if ($query->is_search) {
          $query->set('post_type', array( 'post', 'mycustomposttype1', 'mycustomposttype2', 'mycustomposttype3'));
     }
     return $query;
}

WordPress Hosting

Comments

  1. Hello, This code is work but conflict with bbPress. I add to my functions.php and When I view the topic of bbPress it doesn’t show the content.

Leave a Reply

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

WordPress Snippet Info

Tested with Wordpress v. 4.8
Last Modified: 06.03.2018
Last Reviewed: 06.03.2018

How to include specific custom post types in WordPress search results
3 (60%) 9 vote[s]
Please report broken snippets.
Report Broken Snippet
WordPress Hosting - WP Engine