Include a hidden input field with your search form like this one:
<input type="hidden" name="post_type" value="post" />
Should work, I think.
Otto42, thanks for the reply and that seems like a logical fix, but I had no luck. After adding in that hidden field, the search still returns pages and posts.
Hrm… I thought that would work. Oh well, I’ll have another look at the code.
Looking closer at it, I can’t see why that doesn’t work. Are you sure you put it in the form properly? Got a link so I can see your search results?
I’m sure the hidden field is in the form properly. With a get method on the form the URL output is like this http://*.com/?post_type=post&s=querytext
I can send you the URL, but it’s on a dev site that I’d rather not post publicly. Can I email you?
…Guess not. Well, anyway, the hidden field post_type set to post *does not* work. Try it for yourself. WordPress bug?
Are there any updates on this issue? I too would like the search bar to display only posts.
Someone gave me a great solution. If you use the ‘Search Everything‘ plug-in, you can enter your page id’s to be excluded from the search. Worked great for me, may not be ideal for people with 100+ pages.
I had this problem myself since I mainly use WP as a CMS and wrote this a small, rudimentary plugin:
add_filter('posts_where', 'SearchNoPages');
This adds a filter for the SearchNoPages function below to the posts_where hook.
function SearchNoPages($where)
{
global $wp_query;
if(!empty($wp_query->query_vars['s']))
{
$where .= ' AND (in_posts.post_type=\'post\')';
}
return $where;
}
Now this just adds the post_type=’post’ to the where clause so that posts with post_type=’page’ are excluded.
Put this together in a php file in and save it in your plugins folder (see the documentation on how to make plugins)
Hope this helps.
better remove in_posts.
since you probably use another prefix