# Choose Which Post Types Appear in Search

> Sets exactly which content types the site search returns, for example posts, pages and your portfolio, instead of every public type.

- Type: PHP
- Runs: Run everywhere
- Category: [Search](https://wpide.com/library/category/search/)
- Tags: search, post-types, results
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/include-custom-post-types-in-search/

## Code

```php
// Post types returned by the site search.
$wpide_search_post_types = array( 'post', 'page', 'portfolio' );

add_action( 'pre_get_posts', function ( $query ) use ( $wpide_search_post_types ) {
    if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() || $query->get( 'post_type' ) ) {
        return;
    }

    $types = array_values( array_filter( $wpide_search_post_types, 'post_type_exists' ) );

    if ( $types ) {
        $query->set( 'post_type', $types );
    }
} );
```

## Notes

List the post type names to include. Types that do not exist are ignored.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Choose Which Post Types Appear in Search" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
