0

Here is my case:

I am trying to get all users with some value related to a specific meta key. In other words, I don't want to select users that have nothing on that meta key (mps_finalDate).

So here is my code:

$users = get_users(array(
 'meta_key'     => 'mps_finalDate',
 'meta_value'   => '',
 'meta_compare' => '!=',
 ));

Which works the same as

$users = get_users(array(
  'meta_key'     => 'mps_finalDate',
  ));

But the problem is: this code is selecting users that once had some value on this meta field, even if now they have nothing on it. It seems that the update_user_meta( $user_id, 'mps_finalDate', trim( $_POST['mps_finalDate'] ) ); I am using in my code is not deleting the previous value on the db, thus causing this issue.

In conclusion: the above code is not selecting a user that never had a value on the meta key field mps_finalDate(which is desired), but it is selecting a user that once had a value on it, even if now this value is "".

Any help?

0

It looks like you're using a custom meta field but searching default meta attributes.

https://codex.wordpress.org/Class_Reference/WP_User_Query

WP_User_Query might be a more robust solution.

So it would look like something like :

$args = array(
    'meta_query' => array(
        'relation' => 'AND',
        array(
            array(
                'key'     => 'mps_finaldate',
                'value'   => '',
                'compare' => '!='
            ),
            array(
                'key'     => 'mps_finaldate',
                'value'   => null,

                'compare' => '!='
            )
        )
    )
 );
$user_query = new WP_User_Query( $args );

EDIT:

WP_User_Query is a wrapper for the get_users function you were using but it, as a wrapper, also is going to do the work of the get_userdata function

This is great and detrimental for 2 reasons (each)

Great: it can be hooked (someone edit me) Great: code longevity is sustained because it is core api use

Detrimental: anyone and everyone's plugins can also hook Detrimental: anyone and everyone's plugins can also hook

I'd go the route of using the wp_user_query class if only because the arguments are structured and documented well (but I'll stay for the longevity benefit of the approach being wp_core api)

your mileage may vary

  • Well, your code worked for me. I don't know why mine didn't (isn't get_users() a Wrapper for WP_User_Query)? Anyway, I will stick with yours. Thanks a lot! – Luis Rock Dec 6 '15 at 18:18
  • U have it backwards. wp_user_query is a wrapper for get_users. You can accomplish the Sam thing with get_users but you would need to run the results through get_usermetadata as well in order to compare custom field data. Also - notice the AND clause that was originally omitted. A 0 length string is not the samething as a null – Brandt Solovij Dec 6 '15 at 19:47
  • here's the real rub - get_users is also "probably" more efficient, because the wp_user_query can be hooked and filtered - yes : the best and worst of WP authoring. if you wish to be a purist - push the get_users results through get_userdata but expect ONLY your explicit opinions on the matter - link codex.wordpress.org/Function_Reference/get_userdata edit: also - very welcome! im happy it worked for you:D – Brandt Solovij Dec 7 '15 at 5:03
  • This is confusing... From the Codex, about get_users: Wrapper for WP_User_Query. Retrieves an array of users matching the criteria given in $args.. If one can be hooked and filtered, the other too, isn't? – Luis Rock Dec 10 '15 at 11:07
  • @LuisRock - I'm sorry you are correct regarding the vocabulary, my apologies. WP_User_Query makes use of both get_users and get_userdata - the later is required in order to pass / run queries structured around custom meta values. get_users will only look at core meta fields – Brandt Solovij Dec 10 '15 at 16:46

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.