0

I'm currently struggling with implementing a feature for wordpress site. I would like to get the count of all users which have the key pending_approval='true' and is_photographer='true' (located in wp_usermeta. How could that be achieved? I've been searching through the documentation, but I couldn't find an proper example.

Example code:

$users_sent_to_admin = new WP_User_Query(array(
                                             'meta_query' => array(
                                                 array(
                                                     'key' => 'pending_approval',
                                                     'value' => 'true',
                                                     'compare' => '='),
                                                 array(
                                                     'key' => 'is_photographer',
                                                     'value' => 'true',
                                                     'compare' => '='
                                                 ),
                                             )
                                         ));

$users_sent_to_admin = new WP_Query($users_sent_to_admin);
  • I've just updated my question. This code doesn't return anything for some reason - and I'm sure there is data in the database. I'm not sure if it's correct or syntax. I've copied it from wordpress.stackexchange.com/questions/131351/… – tftd Jan 30 '14 at 19:09
  • 3
    Are you sure the boolean value 'true' is stored as string? Isn't it more likely to be 1? I'm just guessing here as I don't have any pending users. – tfrommen Jan 30 '14 at 19:11
  • Yes, the variable is stored as a string. Is that, though, the correct way to get the data from the database? I'm kind of new to wordpress... – tftd Jan 30 '14 at 19:14
  • 1
    Are you sure there are users matching the query logic? – s_ha_dum Jan 30 '14 at 19:21
  • After I debugged it a bit more, I found out I was just not using the $users_sent_to_admin after that... The code above works just fine. I didn't know I had to call $users_sent_to_admin->get_results() after the query. Previously I was using get_users() and it was returning the users right away, so I presumed the WP_Query would give me the results.... Thank you all for the help! – tftd Jan 30 '14 at 19:28
1

I think you are missing the relation key here. If you want the users to be picked up when both the above meta values are matched, then you should use this as below

$users_sent_to_admin = new WP_User_Query(
                        array(
                            'meta_query' => array(
                                'relation' => 'AND',
                                array(
                                    'key' => 'pending_approval',
                                    'value' => 'true',
                                    'compare' => '='),
                                array(
                                    'key' => 'is_photographer',
                                    'value' => 'true',
                                    'compare' => '='
                                ),
                            )
                        )
                    );
| improve this answer | |
  • Both solutions(yours and mine) return the right content, so I'll accept your answer. – tftd Jan 31 '14 at 22:38

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.