Skip to:
Content
Pages
Categories
Search
Top
Bottom

Limit search on forum


  • jodlajodla
    Participant

    @jodlajodla

    Hello!

    It is possible to limit search’s post type on forum and leave the one on the blog untouched? My current filter is like this:

    function search_filter($query) {
    	if ($query->bbp_is_search) {
    		$query->set('post_type', array('post', 'forum', 'topic', 'reply'));
    	} else if ($query->is_search) {
    		$query->set('post_type', 'post');
    	}
    	return $query;
    }
    add_filter('pre_get_posts', 'search_filter');

    But it doesn’t work… what do I have to change?

    Thank you! :)

Viewing 4 replies - 1 through 4 (of 4 total)

  • Robin W
    Moderator

    @robin-w

    bbpress 2.4.1 comes with it’s own search function, which only searches forums.

    this post also describes forum only search boxes which might help

    http://vfbpro.com/2012/10/25/how-to-add-a-search-box-to-bbpress/

    and also a widget

    http://wordpress.org/plugins/bbpress-search-widget/

    that you could crack the code of


    jodlajodla
    Participant

    @jodlajodla

    You are right. There’s a problem because WordPress filters all searches, including the bbPress. Now I don’t know how to recognize if the search is coming from WordPress’s search box or bbPress’s searchbox.

    I don’t need search box for forum on first/home page, but I need to filter results when searching with WordPress’s search box. I must not show pages in it, so that’s why I’m searching solution.

    Thank you!


    jodlajodla
    Participant

    @jodlajodla

    I found out that I misunderstood your post. I downloaded plugin that you suggested and checked the code. I remembered that I used is_bbpress() somewhere and that was the solution for my problem. There’s the snippet of code that I used in my theme in file functions.php:

    /* Do not search pages, only posts and if there's a request from bbPress or Admin panel than ignore setting of post type */
    function search_filter($query) {
    	if ($query->is_search && !is_admin() && !is_bbpress()) {
    		$query->set('post_type', 'post');
    	}
    	return $query;
    }
    add_filter('pre_get_posts', 'search_filter');

    Thank you very much again! :) :)


    Robin W
    Moderator

    @robin-w

    Great, glad you’re fixed !

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.