Warning: include(/home/pixenpaa/wphats.com/wp-content/advanced-cache.php): failed to open stream: No such file or directory in /home/pixenpaa/wphats.com/wp-settings.php on line 95

Warning: include(/home/pixenpaa/wphats.com/wp-content/advanced-cache.php): failed to open stream: No such file or directory in /home/pixenpaa/wphats.com/wp-settings.php on line 95

Warning: include(): Failed opening '/home/pixenpaa/wphats.com/wp-content/advanced-cache.php' for inclusion (include_path='.:/opt/alt/php72/usr/share/pear') in /home/pixenpaa/wphats.com/wp-settings.php on line 95
How to include custom post type in Search Results » WP Hats

How to include custom post type in Search Results

By default in WordPress, the search results shows for pages & defualt post types. So if you create any custom post type it doesn’t include automatically.

The the_search_query filter helps you to include your custom post types in search results. Just copy and paste the below code by renaming your post_type in your theme functions.php file.

// MAKE CUSTOM POST TYPES SEARCHABLE
function add_search_results( $query ) {
 if ( $query->is_search ) { $query->set( 'post_type', array( 'site', 'plugin', 'theme', 'person' )); } 
 return $query;
}
add_filter( 'the_search_query', 'add_search_results' );

 

This post has already been read 2857 times!

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

More Posts

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

One thought on “How to include custom post type in Search Results

  • May 21, 2015 at 6:15 pm
    Permalink

    The articles is actually excellent, Good process, cheers Mehedi

    Reply

Leave a Reply

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