Skip to content

Instantly share code, notes, and snippets.

@thomasgriffin
Last active December 30, 2019 18:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save thomasgriffin/4159035 to your computer and use it in GitHub Desktop.
Save thomasgriffin/4159035 to your computer and use it in GitHub Desktop.
Add custom post types to search results in WordPress.
<?php
add_filter( 'pre_get_posts', 'tgm_io_cpt_search' );
/**
* This function modifies the main WordPress query to include an array of
* post types instead of the default 'post' post type.
*
* @param object $query The original query.
* @return object $query The amended query.
*/
function tgm_io_cpt_search( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'post', 'movies', 'products', 'portfolio' ) );
}
return $query;
}
Copy link

ghost commented Aug 7, 2017

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment