October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
Custom Post Types

How to Limit WordPress Search Results to Specific Post Types

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To restrict the normal WordPress search page to selected content types, use the pre_get_posts hook and set the main search query’s post_type to the allowed post-type slugs. For a separate search loop, pass those slugs to a new WP_Query. Use exclude_from_search instead when a type should be omitted from ordinary front-end search site-wide.

Filter the main front-end search query

Add this code to a site-specific plugin or a child theme’s functions.php file. Replace book with the slug of your registered custom post type, or remove it if you want to search only posts and pages.

function mysite_limit_search_post_types( $query ) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
        $query->set( 'post_type', array( 'post', 'page', 'book' ) );
    }
}
add_action( 'pre_get_posts', 'mysite_limit_search_post_types' );

pre_get_posts runs after WordPress creates a query and before it executes, so the callback can adjust the query in time. The guards ensure the change applies to the main search query on the front end, rather than affecting dashboard or secondary queries. See the WordPress pre_get_posts reference for the hook and its search-exclusion example.

Choose the allowed types

Use registered post-type slugs—not the labels visitors see in the dashboard. The array above allows standard posts, pages and the custom type whose slug is book. To search only posts and pages, use array( 'post', 'page' ). To allow one type, set post_type to its slug as a string. The WP_Query reference documents post_type as accepting a string or array.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This code replaces the type selection for the matching main query. If another part of your site has already selected types and you need to preserve them, read the current post_type query variable and merge intentionally rather than overwriting it. Confirm the custom type is registered before using its slug.

Use WP_Query for a separate search loop

If you are building a search component or custom results loop rather than changing the site’s main search page, create a separate query and specify both the search term and allowed types:

$search = new WP_Query(
    array(
        's'         => get_search_query(),
        'post_type' => array( 'post', 'page', 'book' ),
    )
);

Here, s supplies the keyword and post_type limits the results. Run this query alongside your own loop; it does not change the main search query. The WP_Query reference includes examples of searching one or multiple post types.

Choose between a per-query filter and site-wide exclusion

Approach Best for Effect
pre_get_posts A contextual rule for the main front-end search Sets the allowed types for queries that match your callback’s conditions.
Separate WP_Query A custom search form or independent results loop Limits only the query you create.
exclude_from_search A post type that should stay out of ordinary front-end search throughout the site Sets search eligibility as part of the post type’s registration.

For a site-wide exclusion, set the custom post type’s exclude_from_search registration argument appropriately. WordPress documents that this flag controls front-end search exclusion. The any value for post_type also excludes types registered with exclude_from_search set to true; it is not a way to include every registered type. Check the WP_Query documentation and the register_post_type reference when configuring the type.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep custom post types maintainable

WordPress recommends registering custom post types in a plugin or must-use plugin rather than a theme. That keeps the content type available if you change themes. Put the search-filter callback in a site-specific plugin or child theme as appropriate for your setup, and make sure its registration and search rules are maintained together. The Plugin Handbook’s custom post types guide covers registration and querying; its Plugin Handbook also describes altering the main query with pre_get_posts.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check WordPress version before changing searched fields

If you also need to control which fields WordPress searches, the current WP_Query reference documents the search_columns parameter for WordPress 6.2 and later. Verify the installed WordPress version before relying on it; limiting post types and limiting searchable fields are separate settings.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read next

Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.