douzhang2680 2019-04-07 14:36
浏览 76
已采纳

按自定义元值分组WP用户查询

I'm trying to query WP Users and group them by billing_state, a custom meta key/value but I'm getting some unexpected results where it gives me some empty options but also is only returning one result per billing_state.

Based on some research on SO it was suggested I query all users and then create a new array where I can set the state as the array key then the user object as the value.

Here is what I tried:

add_shortcode('pro_pro_users','pro_pro_users'); 
function pro_pro_users(){

    global $wpdb; 
    $user_query = new WP_User_Query( array( 'fields' => 'all' ) ); 
    $users = $user_query->get_results(); 

    if (!empty($users)) { 
        $usersByState = array();
        foreach ($users as $user) {
            $usersByState[get_user_meta($user->ID, 'billing_state', true)] = $user;
        }

        foreach ($usersByState as $k => $v){ 
      if(!empty($v)) {
        echo '<li><h5>' . $k . '</h5>';
          echo '<ul>'; 
            foreach ($v as $user) {
               echo '<li><a href="' . get_author_posts_url($user->ID) . '">' . get_user_meta($user->ID, 'first_name', true) . ' ' . get_user_meta($user->ID, 'last_name', true) . ' (' . $user->display_name . ')</a></li>'; 
            }
          echo '</ul>';
        echo '</li>';
      }
        }

      echo '</ul>'; 
    } else { 
       echo 'No users found'; 
    } 
}

Here is what is happening (note the empty ones and only one per state although I know some of these have more than one enter image description here

I've checked:

https://codex.wordpress.org/Class_Reference/WP_User_Query https://codex.wordpress.org/Database_Description#Table:_wp_users https://codex.wordpress.org/Function_Reference/get_users https://wordpress.stackexchange.com/questions/105937/group-posts-by-meta-key https://wordpress.stackexchange.com/questions/205427/group-posts-by-attachment-meta-key https://usersinsights.com/wordpress-user-meta-query/

  • 写回答

1条回答 默认 最新

  • doudou890510 2019-04-07 15:09
    关注

    What comes to my attention is that the following code will "override" each billing-state with the last user found:

      $usersByState = array();
      foreach ($users as $user) {
        $usersByState[get_user_meta($user->ID, 'billing_state', true)] = $user;
      }
    

    From what I understood is that you should rather go for:

     $usersByState = array();
     foreach ($users as $user) {
        $billing_state = get_user_meta($user->ID, 'billing_state', true);
        if ($billing_state)
           $usersByState[$billing_state][] = $user;
     }
    

    So by doing this you would collect several users for different states. Moreover this should take care of empty "states" as you described. Not sure whether this fully solves your issue but it might be a starting point for refining your function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测