Show Only Posts in WordPress Search Results

By default, WordPress search includes posts, pages, and sometimes custom post types. If you’d like to restrict searches to only blog posts, follow these steps.

Step 1: Open your active theme’s functions.php file.

Step 2: Insert this code:

function custom_search_only_posts($query) {
    if ($query->is_search() && !is_admin()) {
        $query->set('post_type', 'post');
    }
}
add_action('pre_get_posts', 'custom_search_only_posts');

Step 3: Save the file. Now, only posts will be shown in WordPress search results — pages and other types will be excluded automatically.

This approach improves search relevance for blog-based websites by limiting the results to what users most likely expect: your blog content.

 

Leave a Comment

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

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.