5

I'm trying to get all users assigned to a custom usermeta. I know that get_users() is supposed to work; however, it doesn't seem to be working. What I have is a usermeta storing a category ID and I'm trying to use 'meta_value' => $cat_id to query the database. So this is the code I have:

$args = array( 'meta_key' => 'custom-usermeta', 'meta_value' => $cat_id ); $users = get_users( $args ); 

and here is how I'm saving it:

update_user_meta( $user_id,'custom-usermeta',$cat_id); 

$cat_id is an array.

But this isn't working. What I've figured out so far is that the meta_value expects an unserialized value. How can I get that to work? I tried the 'meta_compare' => 'LIKE' but then that just about grabs everything (because of the way Wordpress serializes the values). Is there anything else I can do to fix this?

0

2 Answers 2

2

In your original code, you're not passing an operator to meta_compare. Note that get_users() does not define a default operator. Try using '=':

$args = array( 'meta_key' => 'custom-usermeta', 'meta_value' => $cat_id, 'meta_compare' => '=' ); $users = get_users( $args ); 

As a diagnostic, you might make sure that the problem isn't the saving/querying by your custom user metadata. Replace the meta_key and meta_value with one of the default WP user meta keys, just to see if anything is returned. (Easiest might be the user role?)

And this one is a long-shot, but: even though the get_users() Codex documentation says otherwise, from what I can gather from source, the meta_query for WP_user_query should be the same as the meta_query for WP_query - in which case, have you tried putting your meta query in an array? e.g.:

$args = array( array( 'meta_key' => 'custom-usermeta', 'meta_value' => $cat_id ) ); $users = get_users( $args ); 

Cf. WP_Query usage of meta_query.

1
  • Thanks for helping Chip! I tested to see if I was getting the same error with other user_meta as you suggested and I tried wp_capabilities since it was serialized the way my data is and it wouldn't grab that info either. Maybe wordpress can't use the = operator on serialized data? I also tried putting the query in an array, but that just pulled all wp_capabilities, ignoring my query as far as meta-value. Commented Feb 1, 2012 at 13:32
1

You could try to user a "direct" WP_User_Query.

$user_search = new WP_User_Query( array( 'orderby' => 'display_name', 'fields' => 'all_with_meta', 'meta_key' => 'CAT OR WHATEVER KEY NAME', 'meta_value' => $cat_id, 'meta_compare' => '=' ) ); $users = $user_search->get_results(); 

Not tested, but it should work.

Update

As far as I can see, the $meta_value inside update_metadata() (the function that gets wrapped by update_user_meta()), takes a single value. The only way it can get serialized is maybe_serialize();, which only serializes on demand. So the problem must be on your side and the $cat_ID meta value.

You can recheck close before the maybe_serialize() with the following filter:

apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value ) 

{$meta_type} would be user in this case. Just drop your $cat_ID inside some test function to see what's happening.

7
  • I appreciate the answer, but I've tried using the = operator and it didn't work since it's expecting a serialized response. I just tried using the "direct" WP_User_Query too, and unfortunately that didn't work either. Commented Jan 30, 2012 at 13:53
  • Hm. You should show us how you save the user data I guess. Commented Jan 30, 2012 at 18:16
  • See update.(filler) Commented Jan 31, 2012 at 15:06
  • Thanks for still helping. I'm not running through in serialization process. I'm just grabbing the POST data, which is an array, and then calling the update_user_meta function. Wordpress does the serializing own its own. Commented Feb 1, 2012 at 13:37
  • @john Have you checked using the provided filter in the update? Commented Feb 3, 2012 at 5:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.