How to Show Only Blog Posts and Hide Pages in WordPress Search Widget

TL;DR: If you’re using a search widget on your WordPress site, you might have noticed that it shows both blog posts and pages in the results. This happens because the default WordPress search behavior includes both posts and pages. This can be an issue if your site is primarily a blog and you don’t want visitors landing on static pages like “About Us” or “Privacy Policy.” In this guide, I’ll show you three simple ways to adjust your search widget so it displays only blog posts, making the search results more focused and relevant.

Why Exclude Pages from Search Results?

Think about it: When someone searches on your site, they’re probably looking for blog posts, not static pages like your “Contact” page or “Terms and Conditions.” Here’s why excluding pages from search results makes sense:

  • Better Search Relevance: Visitors will find blog content instead of unrelated pages.
  • Improved User Experience: A cleaner search helps users find what they’re looking for faster.
  • SEO Benefits: Pages like “Privacy Policy” don’t need to show up in search results, preventing unnecessary indexing.

Let’s dive into three ways to make this happen.


Method 1: Use a Code Snippet (Best for Performance)

If you’re comfortable adding a small code snippet to your theme, this is the fastest and most efficient way to remove pages from search results.

Steps to Add the Code:

  1. Log in to your WordPress dashboard.
  2. Go to AppearanceTheme File Editor.
  3. Open the functions.php file of your active theme.
  4. Paste the following code at the bottom:
function filter_elementor_search_results($query) {
if ($query->is_search() && !is_admin()) {
$query->set('post_type', 'post'); // Ensures only blog posts appear in search
}
}
add_action('pre_get_posts', 'filter_elementor_search_results');

How This Works:

  • This code checks if the search is happening on the front end of the site.
  • It then tells WordPress to only include blog posts in the search results, excluding pages automatically.

If you want to exclude specific pages instead of all pages, you can modify the code like this:

function filter_elementor_search_results($query) {
if ($query->is_search() && !is_admin()) {
$query->set('post__not_in', array(123, 456)); // Replace 123, 456 with actual page IDs
}
}
add_action('pre_get_posts', 'filter_elementor_search_results');

To find your page IDs, go to PagesAll Pages, hover over a page title, and look for the ID number in the URL.

This method is great if you want a lightweight, permanent solution without relying on extra plugins.


Method 2: Adjust Elementor Pro Settings (No Coding Needed)

If you have Elementor Pro, you can easily modify the search results visually without touching any code.

Steps to Exclude Pages in Elementor Pro:

  1. Go to TemplatesTheme Builder in WordPress.
  2. Open the Search Results template (or create one if it doesn’t exist).
  3. Click on the Posts Widget that displays search results.
  4. In the left sidebar, go to QuerySource and change it to Posts instead of “All”.
  5. Save your changes.

This method is perfect for those who prefer using Elementor’s drag-and-drop interface instead of coding.


Method 3: Use a Plugin (Best for Beginners)

If you’re not comfortable editing code and don’t have Elementor Pro, you can use a plugin like Search Exclude to hide pages from search results.

Steps to Exclude Pages with a Plugin:

  1. Go to PluginsAdd New in WordPress.
  2. Search for Search Exclude, install it, and activate it.
  3. Go to PagesAll Pages in your dashboard.
  4. Edit any page you want to exclude from search.
  5. Look for the “Exclude from Search Results” checkbox (usually at the bottom of the page editor) and check it.
  6. Save your changes.

This is the easiest solution for beginners who want full control over which pages appear in search results without touching code or using Elementor Pro.


Which Method is Best for You?

Here’s a quick rundown to help you decide:

  • For a fast, code-free solution, use the Search Exclude plugin (Method 3).
  • If you have Elementor Pro and prefer a visual approach, adjust the Search Results template (Method 2).
  • If you want a lightweight, code-based solution without extra plugins, use the code snippet (Method 1).

No matter which method you choose, your Elementor search will now only show blog posts, making it more relevant and useful for visitors.

Need Help?

If you have any questions or run into any issues, let me know in the comments below. Also, if this guide helped you, consider sharing it with others who might find it useful!

Get started today by reading our next post on SEO Tips for WordPress or check out our full guide on optimizing search results for even better performance!

Leave a Comment

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

Scroll to Top