Created
March 3, 2017 22:17
-
-
Save hemusyl/f7daca1ae36075af6a862c6e5fb9c8bd to your computer and use it in GitHub Desktop.
Search in Custom Post Types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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