WP_USER_QUERY不按元键排序

时间:2012-10-29 作者:Pollux Khafra

我试图列出按元值排序的用户。我将每个用户的分数存储在他们的用户元中,这只是一个数字,但循环没有排序。我将分数包括在循环中,以便我可以看到它没有被正确排序。我做错了什么?

 $args = array(
    \'meta_key\' => \'score\',
    \'meta_value\' => 0,
    \'meta_compare\' => \'>\',
    \'orderby\' => \'meta_value_num\',
    \'number\' => 20
 );
 $suggested_user_query = new WP_User_Query( $args );
 $users = $suggested_user_query->get_results();
 echo \'<div id="user_suggest">\';
     echo \'<ul>\';
     foreach ($users as $user) {
         // get all the user\'s data
         $user_info = get_userdata($user->ID);
         echo \'<li>\';
     echo \'<h3>\'.bp_core_get_user_displayname( $user->ID ).\'</h3>\';
     $score = get_user_meta( $user->ID, \'score\', true );
         echo $score;
         echo \'</li>\';
     }
     echo \'</ul>\';     
 echo \'</div>\';

1 个回复
SO网友:1naveengiri

您可以尝试此代码

 $args = array(
            \'meta_query\' => array(
                    array(
                        \'key\'     => \'score\',
                        \'value\'   => 0,
                        \'compare\' => \'>\',
                        \'type\'    => \'numeric\'
                    )
            ),
            \'orderby\' => \'meta_value_num\',
            \'number\' => 20
 );

 $suggested_user_query = new WP_User_Query( $args );
 $users = $suggested_user_query->get_results();
 echo \'<div id="user_suggest">\';
            echo \'<ul>\';
                foreach ($users as $user) {
                 // get all the user\'s data
                 $user_info = get_userdata($user->ID);
                 echo \'<li>\';
                echo \'<h3>\'.bp_core_get_user_displayname( $user->ID ).\'</h3>\';
                $score = get_user_meta( $user->ID, \'score\', true );
                 echo $score;
                 echo \'</li>\';
                }
            echo \'</ul>\';     
 echo \'</div>\';
有关更多信息,请查看此链接https://codex.wordpress.org/Class_Reference/WP_User_Query

结束