Wordpress Search Function to only search posts

Wordpress Search Function to only search posts

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Restricting WordPress Search to Posts Only

By default, the native WordPress search mechanism queries both wp_posts and wp_pages. This behavior stems from the fact that in the database schema, pages are stored as post type objects within the wp_posts table, distinguished only by their post_type column. When a user submits a query via the standard search form, WordPress executes an SQL query that ignores this distinction unless explicitly filtered.

To restrict the search results exclusively to blog posts and exclude static pages, you need to modify the underlying SQL query or intercept the request in your theme's logic. The most direct approach involves accessing the WP_Query object and ensuring the post_type parameter is set strictly to 'post'.

Modifying the Search Query Programmatically

If you are building a custom search form or overriding the default search behavior, the cleanest solution is to pass the post_type argument when initializing your query. In most themes, the default search handler uses a global $wp_query. You can target this by hooking into the pre_get_posts action, which fires before the main query runs.

Here is how you implement this in your theme's functions.php file:

add_action( 'pre_get_posts', function( $query ) {
    // Ensure we are modifying the main query to avoid affecting archive pages or widgets
    if ( ! $query->is_main_query() || ! $query->is_search() ) {
        return;
    }

    // Restrict search results to only 'post' type, excluding 'page', 'attachment', etc.
    $query->set( 'post_type', [ 'post' ] );
} );

This code snippet checks two critical conditions: whether it is the main query and whether the current context is a search request. If both are true, it overwrites the post_type parameter. By default, WordPress includes all registered post types in a search unless told otherwise. Setting this to an array containing only 'post' ensures that pages, attachments, or any custom post types you might have created will not appear in the results.

Why Default Behavior Includes Pages

It is worth understanding why WordPress defaults to including pages. Historically, pages were treated as "static content" while posts were "dynamic entries," yet both lived in the same table structure. The search algorithm was designed for breadth-first retrieval by default. Overriding this requires an explicit instruction to narrow the scope. This distinction matters when you have large amounts of static documentation or portfolio items stored as pages; without filtering, a user searching for "about" might expect only your About Page but instead see unrelated blog posts containing that word.

Alternative: Custom Search Form Fields

If you are using a plugin like Advanced Custom Fields (ACF) or building a custom form with Gravity Forms or Contact Form 7, the logic often needs to happen on the submission side rather than the query modification side. In those scenarios, you might need to manually construct a new WP_Query instance after validating the form input.

if ( isset( $_POST['search_query'] ) ) {
    $s = sanitize_text_field( $_POST['search_query'] );
    
    // Explicitly define post type in the custom query arguments
    $args = array(
        'post_type' => 'post', 
        's'         => $s,
        'posts_per_page' => 10
    );

    $custom_search = new WP_Query( $args );
    
    // You would then loop through $custom_search->posts
}

Trade-offs and Considerations

The primary trade-off here is the loss of discoverability for static pages via the main search bar. If your site relies heavily on static documentation where users might expect to find answers by searching terms like "contact" or "privacy," you must weigh whether a dedicated navigation menu is a better UX pattern than relying on search.

Furthermore, remember that this filter applies globally to the main query. If you have custom widgets or sidebars that run their own searches (though rare for pages), they will also be affected unless you scope the hook more tightly. However, standard widgetized search blocks usually respect the global pre_get_posts hook logic if they utilize the same query structure.

For developers managing complex sites with many custom post types, this specific filter is a lightweight way to maintain separation of concerns between dynamic content (posts) and static content (pages) without requiring heavy plugin dependencies. If you prefer a managed solution that handles these edge cases automatically, resources like https://laravelcompany.com offer insights into building robust search architectures using modern frameworks, though native WordPress hooks remain the most efficient path for standard installations.

Our network