0

I try to make a page to list users with a meta values.

I search to get all users with a site url or user_description Thx

0

You can do a meta query using OR relation. After that you use get_users() function to get all the users you selected in the query. Then output their nicename in a list with using foreach.

$your_url = 'https://www.yoururl.com';
$your_description = 'your description';

$args = array(
'meta_query' => array(
    'relation' => 'OR',
    array(
        'key'     => 'user_url',
        'value'   => $your_url,
        'compare' => '='
    ),
    array(
        'key'     => 'description',
        'value'   => $your_description,
        'compare' => '='
    )
)
);
$users_array = get_users($args);
echo '<ul>';
if ($users_array ) {
  foreach ($users_array as $user) {
    echo '<li>'.$user->user_nicename.'</li>';
  }
  echo '</ul>';
} else {
  echo 'nothing found';
}

Another way would be to loop through all users, check the meta values and only output the name, if the value fits. The disadvantage is that you have to go through all users, which can take some time if you have a lot of them. So I just add this as an addition, but would recommend using the meta query.

https://developer.wordpress.org/reference/functions/get_user_meta/

$users = get_users( array( 'fields' => array( 'ID','user_url','user_nicename','description' ) ) );
foreach($users as $user){
    $userurl = get_user_meta ( $user->user_url)); // all the values you need
    if ( $userurl == $your_url ) { echo $user->user_nicename.'<br>'; }
    // and so on
}

EDIT because of comment:

You can order by a meta_key with adding it to your $args array:

$args = array(
'meta_query' => array(
    'relation' => 'OR',
    array(
        'key'     => 'user_url',
        'value'   => $your_url,
        'compare' => '='
    ),
    array(
        'key'     => 'description',
        'value'   => $your_description,
        'compare' => '='
    ),
),
'orderby' => 'user_url'
);

This will work for all kind of fields, you just need the meta_key and the order_by. Here is a nice article of how to use it: https://rudrastyh.com/wordpress/meta_query.html

$args = array(
    'meta_key' => 'license_id',
    'orderby' => 'meta_value'
);

You are also able to choose ASC or DESC order:

'orderby' => array(
        'license_id' => 'ASC'
    ),
| improve this answer | |
  • I think I’ve spoken badly. I created a license_id field, for members of group. I want to display all users with a license, and sort users by license_id. I can see the list of users, but I don’t sort it. Thx – David Sticker Jun 18 at 7:46
  • I added how to "orderby" in my answer, please check if it solves your issue. – rank Jun 18 at 8:53
  • Nice thanks a lot. – David Sticker Jun 18 at 10:20

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.