WordPress.org

Ready to get started?Download WordPress

Forums

2.5 Search Posts Only? (12 posts)

  1. urbanor
    Member
    Posted 6 years ago #

    I have a 2.3 WordPress site that I'd like to upgrade to 2.5, but there's only one issue - I don't want pages included in the search (one of 2.5's new features).

    Anyone know if there's a way to only search posts with 2.5?

  2. urbanor
    Member
    Posted 6 years ago #

    bump!

  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Include a hidden input field with your search form like this one:
    <input type="hidden" name="post_type" value="post" />

    Should work, I think.

  4. urbanor
    Member
    Posted 6 years ago #

    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.

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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?

  6. urbanor
    Member
    Posted 6 years ago #

    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?

  7. urbanor
    Member
    Posted 6 years ago #

    ...Guess not. Well, anyway, the hidden field post_type set to post *does not* work. Try it for yourself. WordPress bug?

  8. urbanor
    Member
    Posted 6 years ago #

  9. KMOGVIDEO
    Member
    Posted 6 years ago #

    Are there any updates on this issue? I too would like the search bar to display only posts.

  10. KMOGVIDEO
    Member
    Posted 6 years ago #

    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.

  11. btsoom
    Member
    Posted 6 years ago #

    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.

  12. btsoom
    Member
    Posted 6 years ago #

    better remove in_posts. since you probably use another prefix

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.