1

I am having a problem with an if statement which I have now tested many times over.

        $user_query = new WP_User_Query( array( 'role' => 'Subscriber' ) );

        if ( !empty( $user_query->results ) ) {
            foreach ( $user_query->results as $user ) {

                echo $user->province . '<br/>'; // this outputs the province as expected, i.e. Free State


                if ($user->province == 'Free State') { // This does not return true, even though the echo above works
                    echo '<strong>' . $user->company_name . '</strong> <br/>';
                    echo $user->town. '<br/>';
                    echo $user->province. '<br/>';
                }


        } else {
            echo 'There are currently no photographers for this region.';
        }

}

I basically only wanting to show photographers in a certain province. Many thanks.

6
  • I see there isn't a correct closing parenthesis for "foreach" loop, can you please verify this – Maruti Mohanty Apr 10 '14 at 7:02
  • 1
    @MarutiMohanty, fixed it. Copy and paste issue – SixfootJames Apr 10 '14 at 8:46
  • It happens, use a better editor/IDE to avoid anything such in future. – Maruti Mohanty Apr 10 '14 at 8:49
  • have you checked that $user->province does not have any unwanted space characters? – Michael Apr 10 '14 at 14:34
  • How do I check that @Michael? Other than echoing out the output? I do see in the developer toolbar that Free State is wrapped in quotes like this "Free State" – SixfootJames Apr 11 '14 at 6:08
0

Try changing this:

if ($user->province == 'Free State') {

to:

if ($user->province === 'Free State') {
4
  • How will this make a difference? – RRikesh Apr 10 '14 at 6:11
  • I've had unexpected results on strings using == and tend to use === in such instances. On 2nd thoughts, it would be more likely to work with == than === so apologies for that. Do you have any other suggestions? – TomC Apr 10 '14 at 6:39
  • It's good practice to use === as it also checks for the type of that variable. But for non empty strings it won't make any difference. stackoverflow.com/questions/1281664/… – RRikesh Apr 10 '14 at 6:47
  • Thanks Tom but that didn't work :/ – SixfootJames Apr 10 '14 at 8:45
0

First I would make sure the output on the echo is exactly as you suspect by wrapping it in <pre> tags, then viewing source on the front end to make sure there are no other special characters involved.

I notice that this post, http://tommcfarlin.com/wp_user_query/ uses $user_query->get_results(); instead of $user_query->results; maybe give that a try?

I've always had problems with doing if statements by name and where possible use id's instead. But it doesn't sound like that is possible here :/

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.