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?
1 Answer
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,
));