Matthew How About (@matthewhowabout)

Forum Replies Created

Viewing 1 replies (of 1 total)
  • In reply to: Search by user

    Matthew How About
    Participant

    @matthewhowabout

    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

Viewing 1 replies (of 1 total)