Skip to:
Content
Pages
Categories
Search
Top
Bottom

Search by user

  • @lukehancock

    Participant

    I managed to implement a custom function to search by $forum_id and it’s all working and great, and it was done using the instructions from this page.

    What I need is a way to search by user. Here’s what the search by forum function looks like:

    function my_bbp_filter_search_results( $r ){
    	//Get the submitted forum ID (from the hidden field added in step 2)
    	$forum_id = sanitize_title_for_query( $_GET['bbp_search_forum_id'] );
    	//If the forum ID exits, filter the query
    	if( $forum_id && is_numeric( $forum_id ) ){
    		$r['meta_query'] = array(
    			array(
    				'key' => '_bbp_forum_id',
    				'value' => $forum_id,
    				'compare' => '=',
    			)
    		);
    	}
    	return $r;
    }
    add_filter( 'bbp_after_has_search_results_parse_args' , 'my_bbp_filter_search_results' );

    I tried changing all instances of the word “forum” to the word “user”, but no dice. I tried doing searches like this:
    http://mysite.com/search/?bbp_search=testword&bbp_search_user_id=123

    But nothing I does works and I’ve spent many hours tearing my hair out trying to get it working. I would hugely appreciate any assistance making it happen.
    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • @matthewhowabout

    Participant

    Hello Wookey,
    Recently I’ve done code for this. This is not the best way to search, because the user can enter a display name instead of user login so it will not work. If someone in the future will know how to get user id from first and(or) last name I’ll be thankful.

    First add this input field to page with search box (remember to put it inside <form> tag)
    <input type="text" name="bbp_search_by_user" placeholder="Input searched username" value="" />

    That snippet doesn’t worked for me, so i made it in different way. I base on @netweb (Stephen Edgar) plugin, which is very similar to sevenspark code. Just added couple things like GET field, add query args if username is set and thats it.

    add_filter ( 'bbp_before_has_search_results_parse_args', 'ha_custom_search_by_username');
     
    function ha_custom_search_by_username() {
    	//Get username from input on search page
    $username = $_GET['bbp_search_by_user'];
    	$default_post_type = array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() );
    	//check if empty
    	if(!empty($username)){
    		//Make ID from username
    		$user = get_user_by('slug', $username);
    		$user_id = $user->ID;
    	$args['author'] = $user_id;	
    	}
    	$args['post_type'] = $default_post_type;
    	$args['orderby'] = 'date';
    	$args['posts_per_page'] = '5';
    	$args['ignore_sticky_posts'] = 'false';
    	$args['order'] = 'DESC';
    
    	return $args;
    }

    Add this to function.php

    @sbmar

    Participant

    I think I almost have this working on my site but I cannot get the form value for some reason:

    $username = $_GET['bbp_search_by_user'];

    If I type the value into the URL (/?bbp_search_by_user=someuser), then it works 100%… should be something silly, right?

    I also added a member drop-down so when someone is searching they can choose from a listing of members that have already created at least one reply:

    global $wpdb;
    
    $sql = "SELECT DISTINCT
    $wpdb->posts.post_author as id,
    $wpdb->users.display_name as name
    FROM
    $wpdb->posts
    INNER JOIN $wpdb->users ON $wpdb->posts.post_author = $wpdb->users.ID
    WHERE
    $wpdb->posts.post_type = 'reply' AND $wpdb->users.display_name <> ''
    ORDER BY
    $wpdb->users.display_name ASC";
    
    $result = $wpdb->get_results($sql, OBJECT);
    			
    echo "<select name=\"bbp_search_by_user\">";
    echo "<option value='ALL'>All Users</option>";
    echo "<option value='ALL'></option>";
    foreach ($result as $key => $value) {			  
       echo "<option value='" . $value->id . "'>" . $value->name . "</option>";
    }
    echo "</select>";

    Anyone know how to get the $_GET[‘bbp_search_by_user’]; to work?

    Here’s the code in my functions.php:

    /* ADD SEARCH BY USER FUNCTION */
    
    add_filter ( 'bbp_before_has_search_results_parse_args', 'ha_custom_search_by_username' );
     
    function ha_custom_search_by_username() {
    	//Get username from input on search page
    	$username = $_GET['bbp_search_by_user'];
    
    	$default_post_type = array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() );
    	//check if empty
    	if(!empty($username) && $username != "ALL"){
    		//Make ID from username
    		$user = get_user_by('slug', $username);
    		$user_id = $user->ID;
    	$args['author'] = $user_id;	
    	}
    	$args['post_type'] = $default_post_type;
    	$args['orderby'] = 'date';
    	$args['posts_per_page'] = '5';
    	$args['ignore_sticky_posts'] = 'true';
    	$args['order'] = 'DESC';
    
    	return $args;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.