If you are working with serialized data as your meta_value, that sucks, as Pieter suggested, it’s best not to.
However if there is no way around the serialization problem then I wonder if a meta_query
using a compare
value of LIKE
might help…
Example:
//psuedo serialized data... (e.g. value of meta_key = fruits_user_like)
//a:4:{i:0;s:5:"apple";i:1;s:6:"orange";i:2;s:6:"banana";i:3;s:31:"serialized_data_sucks_sometimes";}
$users = get_users(
array(
'meta_query' => array(
array(
'key' => 'fruits_user_like',
'value' => array(
'apple',
'orange',
'banana',
'serialized_data_sucks_sometimes'
),
'compare' => 'LIKE'
)
)
)
);
…I wonder.
The above won’t work with a compare
value of LIKE
, we need to use REGEXP
and pass a regular expression as the value.
Try the following:
$args = array(
'meta_query' => array(
array(
'key' => 'fruits_user_like',
'value' => '(apple|orange|banana)',
'compare' => 'REGEXP'
)
),
'count_total' => true
);
$users = new WP_User_Query($args);
print_r( $users->get_total() );
Switch to using to WP_User_Query
, it’s more fitting in this instance because we can pass a count_total
parameter and then call the method WP_User_Query::get_total()