Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active August 27, 2015 18:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tommcfarlin/9d17aad23873bc37d6c4 to your computer and use it in GitHub Desktop.
Save tommcfarlin/9d17aad23873bc37d6c4 to your computer and use it in GitHub Desktop.
[WordPress] Searching custom post types.
<?php
add_filter( 'pre_get_posts', 'acme_filter_search' );
/**
* If the specified query is a search query, then modify the post_type array
* to support a the 'review' custom post type.
*
* @since 1.0.0
*
* @param object $query The current search query.
* @return object $query The modified query supporting CPTs.
*/
function acme_filter_search( $query ) {
if ( ! $query->is_search ) {
return $query;
}
$query->set( 'post_type', array( 'post', 'page', 'review' ) );
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment