0

I want to display 5 users by random who have published more than 10 posts but is not working. What I've done so far is

$args = array(
        'number' => 5 ,
        'meta_key' => 'post_count',
        'meta_value' => 10,
        'meta_compare' => '>',
        'orderby' => 'rand'
     );

$user_query = new WP_User_Query( $args );

if ( ! empty( $user_query->get_results() ) ) {
    foreach ( $user_query->get_results() as $user ) {
         echo  $user->display_name ;
     }
   } else {
              echo 'No users found.';
     }

I appreciate any help from you.

  • The meta query looks off, check e.g. how to construct the meta_query argument. In general random ordering and meta queries do not scale very well.. – birgire Nov 6 '18 at 9:05
0

I can see in your code you did mistakes with meta key arguments. You must use underscore instead of hyphen sign. Here is corrected code:

$args = array(
            'number' => 5 ,
            'meta_key' => 'post_count',
            'meta_value' => 10,
            'meta_compare' => '>',
            'orderby' => 'rand'
            );

            $user_query = new WP_User_Query( $args );

            if ( ! empty( $user_query->get_results() ) ) {
                foreach ( $user_query->get_results() as $user ) {

                    echo  $user->display_name ;

                }
            } else {
                echo 'No users found.';
            }

Hope this will work!

  • Thx for the answer. When I changed to underscore, the output is No users found. . Why no results at all? – p oneland Nov 6 '18 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.