[Coding Tutorial] Include custom post types in the WordPress Search Results
I will start today a series of posts that will teach you some insider secrets about WordPress. I hope you will enjoy it. Any suggestions about future post topics are welcome!
Below is a must do code for anyone working with custom post types in WordPress. It includes those custom post types in the search results of WordPress.
// MAKE CUSTOM POST TYPES SEARCHABLE
function searchAll( $query ) {
if ( $query->is_search ) { $query->set( 'post_type', array( 'site', 'plugin', 'theme', 'person' )); }
return $query;
}
add_filter( 'the_search_query', 'searchAll' );
Now, the code bellow, will also add your custom post types to your sites main RSS feed
// ADD CUSTOM POST TYPES TO THE DEFAULT RSS FEED
function custom_feed_request( $vars ) {
if (isset($vars['feed']) && !isset($vars['post_type']))
$vars['post_type'] = array( 'post', 'site', 'plugin', 'theme', 'person' );
return $vars;
}
add_filter( 'request', 'custom_feed_request' );
