I do work on filter posts by current user, but in my dropdown list, i can get only all users list, but I need to see only the current user
function getСurrentUserForFilter(){
$res = '';
$user_query = new WP_User_Query(array('number'=>999));
$users = $user_query->get_results();
foreach($users as $user){
echo'<option value="'.$user->ID.'">'.$user->user_email.'</option>';
}
return $res;
}
what am I doing wrong?
I do work on filter posts by current user, but in my dropdown list, i can get only all users list, but I need to see only the current user
function getСurrentUserForFilter(){
$res = '';
$user_query = new WP_User_Query(array('number'=>999));
$users = $user_query->get_results();
foreach($users as $user){
echo'<option value="'.$user->ID.'">'.$user->user_email.'</option>';
}
return $res;
}
what am I doing wrong?
Share Improve this question asked Jun 16, 2020 at 12:06 LerryLerry 1111 gold badge2 silver badges10 bronze badges1 Answer
Reset to default 2If you just want to write a single <option>
for the current user, you can get the user object from wp_get_current_user() - you don't need a WP_User_Query at all:
function getСurrentUserForFilter() {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
echo '<option value="'.$user->ID.'">'.esc_html($user->user_email).'</option>';
}
}