I think I’m missing a step here, but for the life of me I can’t see it. I’m trying to display a simple list of users who all have the same role and have the same meta data value for a certain key – namely, teachers associated with a school.
Here is my code:
<div class="teachers-list">
<h3>Your School's Teachers</h3>
<ul>
<?php
$champion_user = wp_get_current_user();
$school = $champion_user->school;
// echo $school just to test it's right.... ?>
Your school: <?php echo $school; ?>
<?php
$args = array(
'role' => 'subscriber',
'meta_key' => 'school',
'meta_value' => $school
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo '<li>' . $user->display_name . '</li>';
}
} else {
echo '<li>No teachers found.</li>';
}
?>
</ul>
</div>
The school variable echoes properly, but then in the WP_User_Query I only get the “no teachers found” result. If I comment out the meta_key and meta_value fields, I get a list of subscribers, so that far works. If I manually code in the meta_value I’m looking for – in this case, “school_one”, it still returns “no teachers found”. I checked the usermeta table in the db and all the users have the school meta key and the proper school values there, so… I’m stumped.
I’ve read and re-read the codex for both WP_User_Query() and get_user_meta(), and written this out both with the pre-WP 3.7 syntax and now the current syntax, but clearly I’ve missed the main point to get this working. Do I need to reset the query somehow? Do I need the “compare” argument too? Am I using the $school variable improperly in the arguments? I’ve tried so many methods I’m not sure what’s left.
Any help is greatly appreciated. Thank you in advance for reading.