Reputation: 67
I'm trying to query WP users that have the current post's ID saved in a meta_key (training_event_attending) with an array of values.
When I do a print_r on the current user's training_event_attending where 10, 11 and 12 are the correct post IDs saved to the user, I get:
$training_event_attending) = get_user_meta($current_user->ID, 'training_event_attending', TRUE);
print_r($training_event_attending);
//Shows Array ( [0] => 10 [1] => 11 [2] => 12 )
Now if I am on post with ID 10, I want to query all users with training_event_attending matching this post ID (10). This is my query:
$event_id = get_the_id();
$user_args = array(
'meta_query' => array(
array(
'key' => 'training_event_attending',
'value' => array($event_id),
'compare' => 'IN',
),
)
);
$assigned_users = get_users( $user_args );
print_r($assigned_users);
//shows an empty array.
I've also tried to serialize value and used 'LIKE' to compare like such:
value => serialize(array($event_id)),
This returns the same user but repeated 3 times.
What am I doing wrong? Can you please point me in the right direction?
Upvotes: 1
Views: 1548
Reputation: 67
OK so I found the solution for anybody who is interested:
$event_id = get_the_id();
$user_args = array(
'meta_query' => array(
array(
'key' => 'training_event_attending',
'value' => serialize($event_id),
'compare' => 'LIKE',
),
)
);
$assigned_users = new WP_User_Query( $user_args );
Upvotes: 3