Skip to:
Content
Pages
Categories
Search
Top
Bottom

how to customize search results


  • smith512
    Participant

    @smith512

    After user searched key wards in one of the parent forums, I want to suggest topics that are a member of the parent forum.
    For example,
    Forum Top |- Forum B |-topic A
    | |-topic F
    |
    |- Forum C |-topic A
    |-topic Z

    If an user now in Forum B searches topic A, only show topic A in Forum B, not Forum C’s topic A.

    What I tried
    First, I add this cord to my child theme’s function.php.
    `function my_bbp_search_form(){
    ?>
    <div class=”bbp-search-form”>

    <?php bbp_get_template_part( ‘form’, ‘search’ ); ?>

    </div>
    <?php
    }
    add_action( ‘bbp_template_before_single_forum’, ‘my_bbp_search_form’ );`

    Next, Add a field to the search form. I override form-search.php in my child theme’s /bbpress directory.

    if ( bbp_allow_search() ) : ?>
    <?php $forum_id = bbp_get_forum_id(); ?>
    <div class="bbp-search-form">
    	<form role="search" id="bbp-search-form" method="get" action="">
    		<input type="text" value="" placeholder="Search <?php the_title(); ?> Topics"  name="s"/>
    		<input type="hidden" name="forum_id" value="<?php echo $forum_id; ?>"/>
    		<input type="submit"/>
    	</form>
    </div>

    Finally, I filter the search query to only search a specific forum. I add this code to my functions.php.

    
    <?php 
    function my_bbp_filter_search_results( $r ){
    	$s=htmlspecialchars($_GET['s'], ENT_QUOTES, 'UTF-8');
    	$forum_id = htmlspecialchars($_GET['forum_id'], ENT_QUOTES, 'UTF-8');
    if(isset($s)){
    	$args=array(
    	's'->$s,
    	'post_parent'->$forum_id,
    	);
            }
            return $r;
    }
    ?>

    However, it didn’t work so I want to know how to achieve this.

    In addition, I challenged another way like this.

    if(bbp_has_topics(array('s'->$s, 'post_parent'->$forum_id)))
            bbp_get_template_part('bbpress/loop', 'topics');

    This code is that I edited above last additional functon “my_bbp_filter_search_results()” in function.php.
    However, I can’t get search’s value(name=’s’) in the first place. I tried to use “echo $s;”, $s didn’t output the value. Where is the search results file of bbpress?

    Please advise me. Thanks for reading.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Your code doesn’t look complete. You posted ‘my_bbp_filter_search_results’, but there is no filter it is tied into. Which filter did you use to hook this function too?

    I can recommend you trying my advanced search plugin: GD Power Search for bbPress – https://wordpress.org/plugins/gd-power-search-for-bbpress/ it allows you to select forums to search through and other things.

    Milan


    smith512
    Participant

    @smith512

    Sorry, I forgot to add a code.
    add_filter( 'bbp_after_has_search_results_parse_args' , 'my_bbp_filter_search_results' );

    Thank you for pointing it out.


    smith512
    Participant

    @smith512

    I am worried about that “GD Power Search for bbPress” is not verified my wordpress version.
    Do you intend to update this?

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