How to include specific custom post types in WordPress search results – WP-Snippet.com How to include specific custom post types in WordPress search results – WP-Snippet.com

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; }

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;

}

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.6 (72%) 5 votes

Please report broken snippets.

Report Broken Snippet wpe-300x250-fourpoints-3-5902887