WP_User_Query not returning users with meta - what am I missing?

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"
            h3Your 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 'liNo 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.

Topic wp-user-query user-meta Wordpress

Category Web


I finally sorted this out - there was a problem in the way that the db was storing the meta_value for my custom key. I had to repair the usermeta db table - I used a database checking plugin and then did the repair through phpMyadmin and now the query works properly. Thank you to anyone who took a look at this.


Try using Subscriber, with capital S.

$args = array( 
    'role' => 'Subscriber',
    'meta_key'    => 'school',
    'meta_value'  => $school
);

:)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.