Skip to:
Content
Pages
Categories
Search
Top
Bottom

Search only topics not forum / replies


  • pluus
    Participant

    @pluus

    Hi,

    I’d like to have a search box that only searches topics but forum & replies. What file or hooks should I change? Please enlighten me 🙁 Thanks.

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

  • pluus
    Participant

    @pluus

    Hmph… okay… I’ve noticed that /plugins/bbpress/includes/search/template.php is actually responsible for that feature:

    
    function bbp_has_search_results( $args = '' ) {
    	global $wp_rewrite;
    
    	/** Defaults **************************************************************/
    
    	$default_post_type = array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() );
    
    	// Default query args
    	$default = array(
    		'post_type'           => $default_post_type,         // Forums, topics, and replies
    		'posts_per_page'      => bbp_get_replies_per_page(), // This many
    		'paged'               => bbp_get_paged(),            // On this page
    		'orderby'             => 'date',                     // Sorted by date
    		'order'               => 'DESC',                     // Most recent first
    		'ignore_sticky_posts' => true,                       // Stickies not supported
    		's'                   => bbp_get_search_terms(),     // This is a search
    	);
    
    

    So, changing ‘post_type’ => $default_post_type to ‘post_type’ => bbp_get_topic_post_type() works. However, it’s not clearly the best practice to modify the plugin directly anyother way to override this?


    Robin W
    Moderator

    @robin-w

    yes put this in your functions file – untested but should work

    add_filter ('bbp_before_get_search_terms_parse_args', 'pluus_amend_search') ;
    
    function pluus_amend_search ($args) {
    	$args['post_type'] =  bbp_get_topic_post_type() ;
    return $args ;
    }

    pluus
    Participant

    @pluus

    Whoa! Thanks!


    Robin W
    Moderator

    @robin-w

    great – glad you are fixed !


    stoffer_dev
    Participant

    @stoffer_dev

    Hi Robin W 🙂

    The above solution does not work in 2019. Is there a new way to only search topics and replies as of now ?


    Robin W
    Moderator

    @robin-w

    the function should still work – it just searches topic.

    when you say ‘The above solution does not work in 2019’ – what exactly do you mean, it errors, it display what ?


    stoffer_dev
    Participant

    @stoffer_dev

    I put this:

    add_filter ('bbp_before_get_search_terms_parse_args', 'pluus_amend_search') ;
    
    function pluus_amend_search ($args) {
    	$args['post_type'] =  bbp_get_topic_post_type() ;
    return $args ;
    }

    in my wp-content/themes/<ThemeName>/functions.php

    But Forum names (“bbp_get_forum_post_type()”) are still included in the search results on the frontend.

    Only editing “plugins/bbpress/includes/search/template.php” works 🙂


    Robin W
    Moderator

    @robin-w

    hmmm. can’t say why, but if you have the template solution, not worth me working out why


    stoffer_dev
    Participant

    @stoffer_dev

    The problem is, using the template solution will get overwritten when I update the the bbPress plugin…:(


    Robin W
    Moderator

    @robin-w

    no, you just put the templates in your child theme

    You can copy all the templates across, but you only need to copy those that you want to change, and it is better just to do this, as then you know which you have altered.

    so if you wanted to amend loop-single-forum you would do the following

    create a directory on your theme called ‘bbpress’
    ie wp-content/themes/%your-theme-name%/bbpress

    where %your-theme-name% is the name of your theme

    find
    wp-content/plugins/bbpress/templates/default/bbpress/loop-single-forum.php
    Make a copy of this file, and put in in the directory called bbpress that you created above, so you end up with
    wp-content/themes/%your-theme-name%/bbpress/loop-single-forum.php
    bbPress will now use this template instead of the original
    and you can amend this


    stoffer_dev
    Participant

    @stoffer_dev

    Can I also override files in the “includes” folder (bbpress/includes/search/template.ph) in a child theme?


    Robin W
    Moderator

    @robin-w

    sorry, I’d missed that you were looking at function templates not output templates – sorry it has been a long day.

    what function in that template are you changing ?


    stoffer_dev
    Participant

    @stoffer_dev

    No worries 🙂 I am just happy to get help.

    I want to change the function: bbp_has_search_results. Where I replace:

    $default_post_type = array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() );

    With:

    $default_post_type = array(bbp_get_topic_post_type(), bbp_get_reply_post_type() );


    stoffer_dev
    Participant

    @stoffer_dev

    If I could put the solution in a custom plugin it would be really awesome 🙂


    Robin W
    Moderator

    @robin-w

    ok, a better filter might be – try adding this to your functions file

    add_filter ('bbp_before_has_search_results_parse_args', 'rew_amend_search') ;
    
    function rew_amend_search ($args) {
    	$args['post_type'] =  bbp_get_topic_post_type() ;
    return $args ;
    }

    stoffer_dev
    Participant

    @stoffer_dev

    Really nice. Worked like a charm 🙂 !

    Can I put this in a custom plugin instead of functions.php ?


    stoffer_dev
    Participant

    @stoffer_dev

    I found a way to put it in a plugin 🙂

    I Thought I would have to hook into ‘plugins_loaded’ like so:

     add_action( 'plugins_loaded', 'bbpress_overwrite' );
    
     function bbpress_overwrite() {
         add_filter ('bbp_before_has_search_results_parse_args', 'rew_amend_search') ;
     }
    
    function rew_amend_search ($args) {
    	$args['post_type'] =  bbp_get_topic_post_type() ;
    return $args ;
    }

    But for some reason this was enough: ?

    add_filter ('bbp_before_has_search_results_parse_args', 'rew_amend_search') ;
    
    function rew_amend_search ($args) {
    	$args['post_type'] =  bbp_get_topic_post_type() ;
    return $args ;
    }

    Robin W
    Moderator

    @robin-w

    not quite sure what you are trying to achieve – where are you putting this code ?


    stoffer_dev
    Participant

    @stoffer_dev

    In a custom plugin I just made 🙂


    Robin W
    Moderator

    @robin-w

    then yes the code I sent is fine and works and is enough !!


    stoffer_dev
    Participant

    @stoffer_dev

    Thanks again 🙂


    Robin W
    Moderator

    @robin-w

    you’re welcome

Viewing 22 replies - 1 through 22 (of 22 total)
  • You must be logged in to reply to this topic.
Skip to toolbar