Search only posts (not pages) in WordPress

To search for only posts (not pages) in WordPress, you can make use of the pre_get_posts filter and modify the main query before it is executed. Here’s an example of how you can achieve this:

1. Open the theme’s functions.php file in your WordPress theme directory.
2. Add the following code to the file:

 

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

3. Save the changes and upload the modified functions.php file back to your server.

 

This code hooks into the pre_get_posts action and modifies the query’s post_type parameter to include only posts when a search is being performed and not when the request is made from the WordPress admin area.

After making these changes, the search results will only display posts and pages, excluding any other post type from the results.

Related Blogs

How to know “Who created this coupon?”

What to do when your client ask “Who created this coupon?” . in WooCommerce.  But we don’t have any default things in WooCommerce  which automatically show author of coupon .

Request A Quote

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.