最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filters - Get current user data

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 2

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论