Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Search Function to only search posts

I want to use the wordpress search function but I want it to only search my blog posts and exclude my static pages from the query. How to do this? I am not opposed to using a plugin.

like image 653
Davey Avatar asked Nov 16 '10 20:11

Davey


People also ask

How do I restrict search results in WordPress?

The easiest way to limit search to specific post types in WordPress is to use a plugin like SearchWP. SearchWP is the best WordPress search plugin on the market, with over 30,000 active installs. Using it, you can limit search on your site to specific post types in just a few clicks, no coding needed.

How do I customize my WordPress search?

The easiest way to customize your WordPress search results page is to use a plugin like SearchWP. This is a flexible and easy-to-use plugin designed to improve WordPress search on your website. After the installation, you'll get full control over how search works on your site and can easily customize it.

How do I use WordPress extended search?

WP Extended Search You can simply go to the plugin settings and select the options that you want to include in the search. You can search in author name, taxonomies, post types, meta data, and more. The plugin extends the default WordPress search so you don't need to add any shortcode or widget.


3 Answers

Answer is here

http://www.shilling.id.au/2011/06/23/wordpress-search-only-show-post-and-not-pages/

<?php
function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('post_type', 'post');
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter');
?>
like image 127
Dean Oakley Avatar answered Oct 08 '22 02:10

Dean Oakley


Simply add <input type="hidden" name="post_type" value="post" /> to the theme search form... regards!

like image 37
Marcos Avatar answered Oct 08 '22 02:10

Marcos


This solution makes the search retrieve only posts if you haven't specified a different post type. This method wont interfere if you specify a custom post type in a hidden field in a different search field.

function searchFilter($query) {
    if ($query->is_search) {
        if ( !isset($query->query_vars['post_type']) ) {
            $query->set('post_type', 'post');
        }
    }
    return $query;
}
add_filter('pre_get_posts','searchFilter');
like image 44
Gustavo Avatar answered Oct 08 '22 04:10

Gustavo