Search WordPress Posts Only (and Exclude Other Pages)

Search posts only in WordPress

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this quick tutorial, you’ll learn how to show only posts in your WordPress search results. In other words, all other pages, categories, and tags will be excluded from your site search.

By default, WordPress search queries all content including pages and posts. In many cases, bloggers want to exclude pages from their search results.

With a few lines of easy code, this fix can be quickly implemented on your WordPress website. Don’t let the fact that we’re working with code scare you. I’ll walk you through the process step-by-step.

WordPress site search only posts

Search WordPress Posts Only

The following code will return just posts when a site search is conducted on a WordPress website.

if (!is_admin()) {
    function wpb_search_filter($query) {
        if ($query->is_search) {
            $query->set('post_type', 'post');
        return $query;

The recommended place to insert this snippet of code is within a child theme, specifically in the function.php file. If you’re using GeneratePress, I have a detailed tutorial here on installing a child theme for GeneratePress.

Another option is to use a plugin like Code Snippets which allows you to run PHP code on your website. You never want to edit your theme directly as this comes with a risk of breaking things. It’s much safer to use a plugin or work within your child theme instead.

After inserting the above code, save your changes. Now when you perform a site search, pages are excluded from the results.

YouTube video

If you have any questions about filtering WordPress site search results, let me know in the comments below. I know a lot of times, bloggers are a bit timid to add code to their website. Don’t worry, if you follow the steps above, I assure you that you won’t break anything.

If you found this blog posts valuable, subscribe to my YouTube channel for all my latest WordPress video tutorials and walkthroughs. My goal is to help website owners like you avoid having to pay thousands of dollars to hire a web developer though my free content.


Meet Tony

Tony from Tony Teaches Tech headshot

With a strong software engineering background, Tony is determined to demystify the web. Discover why Tony quit his job to pursue this mission. You can join the Tony Teaches Tech community here.

Leave a Reply

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