Skip to content

Instantly share code, notes, and snippets.

@hemusyl
Created March 3, 2017 22:17
Show Gist options
  • Save hemusyl/f7daca1ae36075af6a862c6e5fb9c8bd to your computer and use it in GitHub Desktop.
Save hemusyl/f7daca1ae36075af6a862c6e5fb9c8bd to your computer and use it in GitHub Desktop.
Search in Custom Post Types
https://developer.wordpress.org/themes/template-files-section/custom-post-type-template-files/
By default search results do not include custom post type content into the search results. The code can be appended to functions.php to include CPT in search results :
// Show posts of 'post', 'page', 'acme_product' and 'movie' post types on home page
function search_filter( $query ) {
if ( !is_admin() && $query->is_main_query() ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'post', 'page', 'acme_product', 'movie' ) );
}
}
}
add_action( 'pre_get_posts','search_filter' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment