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'd like to use get_users() to return all users that have a particular meta_key, but I don't want to specify what the value has to be because it will change for every user. The value is a json string. Is this possible?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You can use the just the meta_key argument, the result will be similar to using the EXISTS sql statement.

<?php
$users = get_users(array(
    'meta_key'     => 'your_meta_key',
));

Alternatively, you can use an empty string for meta_value (the default) and > for meta_compare. The result is the same (probably because meta_value gets ignored if its empty!).

<?php
$users = get_users(array(
    'meta_key'     => 'your_meta_key',
    'meta_value'   => '',
    'meta_compare' => '>',
));

The first method works when using WP_Query and meta_query as well. Example from a plugin of mine:

    $links = get_posts(array(
        'post_type'   => self::POST_TYPE,
        'numberposts' => apply_filters('seoal_number_links', -1),
        'meta_query'  => array(
            'relation' => 'AND',
            array(
                'key'     => self::get_key("type_{$post->post_type}"),
                'value'   => 'on',
                'compare' => '='
            ),
            array(
                'key'     => self::get_key('url'),
                'compare' => 'EXISTS' // doesn't do anything, just a reminder
            ),
            array(
                'key'     => self::get_key('keywords'),
                'compare' => 'EXISTS' // doesn't do anything, just a reminder
            )
        ),
        'suppress_filters' => false,
    ));
share|improve this answer
add comment

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.