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">
            <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.

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

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

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.