0

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.

0

Try using Subscriber, with capital S.

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

:)

  • Thanks @mukto90 - unfortunately it doesn't change the output, still "no teachers found" and I know I've got 4 subscriber users on the site for dev purposes. Again, if I comment out the meta_key and meta_value lines, the code returns all these subscribers just fine, so it's something about that meta that's throwing me off. – Shannon Mac Apr 3 '15 at 23:53
0

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.

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.