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.
Leave a Reply
You must be logged in to post a comment.
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.
You must be logged in to post a comment.
Answer is here
http://www.shilling.id.au/2011/06/23/wordpress-search-only-show-post-and-not-pages/
Simply add
<input type="hidden" name="post_type" value="post" />
to the theme search form… regards!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.
This wp forum page has a bunch of different examples of how to do this depending on where you want to edit your site (index.php or wp-includes/query.php are your options I believe):
http://wordpress.org/support/topic/possible-search-only-posts-exclude-pages
The outlined solutions are poor. Editing the core impedes on upgradeability of the WordPress install – you’ll have to repeat that change every time you update, which you should be doing. The other solution places unnecessary load on the database by filtering results after retrieving them. The better solution:
In your theme’s functions.php, add a new function to write out a search form:
Now, in the template where you want the form (or within any widgets you’ve created, this could easily be registered as a widget instead), this:
The arguments could be left out from the function & call, but I find them helpful. The key to this whole thing is the hidden input ‘post_type’, which passes the value to the query. The default,
post
, will ensure only posts are returned.Try this one and tell me if works.
Merge the query with global query.
You can use WP Search http://wpsear.ch/ and you can configure which post types to show in results.
If using multiple search forms, the following code is useful to limit a specific form to
post_type
post.For more details: https://wordpress.org/support/topic/limit-a-specific-search-form-to-post-only/