1

The following function will display a list of users based on their meta data:

<?php function get_users_by_meta_data( $meta_key, $meta_value ) {

    $user_query = new WP_User_Query(
        array(
            'meta_key'    =>    $meta_key,
            'meta_value'    =>  $meta_value
        )
    );

    $users = $user_query->get_results();

    return $users;

} ?>

I'm using the function like this, where "school_division" is the meta_key and "Richmond" is the meta_value:

<h2>REGION 2</h2>
<ul>
    <?php foreach( get_users_by_meta_data( 'school_division', 'Richmond') as $user ) { ?>

        <li><?php echo $user->first_name; ?> <?php echo $user->last_name; ?></li>  

    <?php } ?>
</ul>

This correctly lists all the users in the Richmond school division. But I don't want to have to specify "Richmond" (the meta_value). I want to iterate through each meta_value in the "school_division" meta_key, and then list the associated users. So the output would be something like:

Richmond

  • Jane Doe
  • John Doe

Chicago

  • Julie Doe
  • Jim Doe
  • Jeff Doe

New York

  • Joe Doe
  • Jenny Doe

I think I need to add another foreach loop. But I'm not sure how to rewrite the function to do all that. Perhaps the function I'm using is backwards for this purpose. Thank you for any help!

****** UPDATE *********

I've made some changes that are hopefully more efficient (?), so below is my updated code. It returns all the users in Region 2, grouped by their School Division city.

<?php function get_users_by_meta_data( $region, $division ) {

$user_query = new WP_User_Query( 
    array(
        'meta_query'    => array(
            'relation'  => 'AND',
            array( 
                'key'     => 'REGION',
                'value'   => $region,
            ),
            array(
                'key'     => 'SCHOOL_DIVISION',
                'value'   => $division
            )
        )
    ) 
);

// Get the results from the query
    $users = $user_query->get_results();

    return $users;
}
?>

<h1> REGION 2</h2>
<?php 

$cities = $wpdb->get_col("SELECT DISTINCT meta_value FROM $wpdb->usermeta WHERE meta_key = 'SCHOOL_DIVISION'" );

foreach ( $cities as $city ) { ?>

    <h2><?php echo $city; ?></h2>
    <ul><?php
    foreach ( get_users_by_meta_data( '2', $city ) as $user ) { ?>
        <li><?php echo $user->first_name; ?> <?php echo $user->last_name; ?></li>  
    <?php } ?>
    </ul><?php
}
?>    
  • I cannot code now, but the answer you would be looking for would involve getting a dynamic list of all the meta values for the specific key. I also think of getting all the data and then use usort() to sort the results – Pieter Goosen Apr 14 '15 at 4:36
  • It would be far more efficient to query all users in one go, loop over them to sort into groups, and then loop over again to output. – TheDeadMedic Apr 14 '15 at 8:45
  • I updated my code to show a working answer, though I'm not sure if it's very efficient? – LBF Apr 14 '15 at 18:16
0

What about putting the city/regions/whatever into an array you can loop through? Something like this:

$cities = array( 'Richmond', 'Chicago', 'New York' );
foreach ( $cities as $city ) { ?>

    <h2><?php echo $city; ?></h2>
    <ul><?php
    foreach ( get_users_by_meta_data( 'school_division', $city ) as $user ) { ?>
        <li><?php echo $user->first_name; ?> <?php echo $user->last_name; ?></li>  
    <?php } ?>
    </ul><?php
}
  • I would do this but you should get $cities by querying all existing key values first – Bysander Apr 14 '15 at 11:20
  • I tried this: $cities = $wpdb->get_col("SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = 'school_division'" ); but this is just returning the first city twice like: array(4) { [0]=> string(8) "Richmond" [1]=> NULL [2]=> NULL [3]=> string(8) "Richmond" } – LBF Apr 14 '15 at 15:41
  • I just learned about SELECT DISTINCT. Once I added DISTINCT that solved my issue. Thanks! – LBF Apr 14 '15 at 18:14

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.