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
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;
}