Search custom post types (CPTs)

I’m trying to enable my search box to search through my CPTs. All CPTs are set to public and I’m using this code to hook into search to change the post type of the query:

function search_all($query) {
  if($query->is_search) {
    $query->set('post_type', array(
  return $query;
add_filter('pre_get_posts', 'search_all');

The search works but only partially. Results only display one of my CPTs. I am using nice-search and would like to maintain that if possible. Any help would be much appreciated. Thanks!

There are some ideas on this at the post listed below, though most just disable nice search.

The code in the [codex][1] should work. You also need to list the CPTs individually by name.

I know, I’m looking to keep nice search enabled while also searching the ‘post’ post type. I want to add my CPTs.

@Foxaii I tried that function from the codex too but unfortunately it still only returns 1 custom post type result when I know there should be at least 2.

Just tested this and it works for me. Maybe a setting with your CPT you missed?

Here is my CPT code:

register_post_type('collection', array(
  'label' => 'collections',
  'labels' => array(
    'name' => 'Collections',
    'singular_name' => 'Collection',
    'add_new' => 'Add Collection',
    'add_new_item' => 'Add New Collection',
    'edit_item' => 'Edit Collection',
    'new_item' => 'New Collection',
    'view_item' => 'View Collection',
    'search_items' => 'Search Collections',
    'not_found' => 'No collections found',
    'not_found_in_trash' => 'No collections found in Trash'
  'description' => 'The Collections',
  'public' => true,
  'menu_position' => 5,
  'supports' => array(
  'taxonomies' => array(
  'has_archive' => true,
  'rewrite' => array(
    'slug' => 'the-collection',

Looks good, and works for me. Do you have collection vs collections in your filter?

No, it’s collection in the CPT and in the filter.

If your spelling is right everywhere, then it must be a conflict with other theme functions/plugins. Dropped the code as you have it into a new test site and it works just fine.

…and it’s official: I’ve been working on this project for too long.

It slipped my mind that I edited the results grid to only display results with featured images and sure enough, each CPT type I tested only has one example post with a featured image.

I assigned featured images to more CPTs and the search works as expected (I suspect it has been this whole time). At least I know for certain that my featured image if statement is working! :wink:


Glad you got it sorted :slight_smile: