Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);
share|improve this question
1  
What have you tried? –  Mark Kaplun Jan 30 at 19:06
    
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 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. –  t f Jan 30 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 at 19:14
1  
Are you sure there are users matching the query logic? –  s_ha_dum Jan 30 at 19:21

1 Answer 1

up vote 1 down vote accepted

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' => '='
                                ),
                            )
                        )
                    );
share|improve this answer
    
Both solutions(yours and mine) return the right content, so I'll accept your answer. –  tftd Jan 31 at 22:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.