Reputation: 31
I'm using a plugin called Userpro to give extra meta info to my users. One field in particular, called business_category
stores a list of categories for each user in an array. The information in the database looks a little like this a:2:{i:0;s:17:"-Entrepreneurship";i:1;s:11:"-Leadership";}
.
I know that it is possible to query WP users with get_users( $args );
however since the field I am querying is an array, I'm not really sure where to go from here.
Any help is hugely appreciated. Thanks!
Upvotes: 0
Views: 4870
Reputation: 31
Here's the code that did the trick, very similar to what another user posted above.
$args = array(
'meta_query' => array(
array(
'key' => 'business_category',
'value' => 'entrepreneurship',
'compare' => 'LIKE'
)
)
);
Upvotes: 1
Reputation: 5062
If you were in control of how the data is stored, then the obvious suggestion would be to split the information into separate meta keys. However, since that's not the case, we have to use an uglier approach to this problem. I've done this in the past and I'm not happy with it, but sometimes you just have to do it.
Note, that eventually this query might start weighing down your system as the usermeta
table grows. So eventually you might have to find a way to split this information into separate meta_keys, or just store the business_category
separately.
function get_users_by_business_category( $category ) {
return get_users( array(
'meta_query' => array(
array(
'key' => 'business_category',
'value' => gen_sql_like( false, $category ),
'compare' => 'LIKE',
)
),
) );
}
function gen_sql_like( $key = false, $value ) {
if ( $key ) {
return str_replace( array( 'a:1:{', '}' ), array( '', '' ), serialize( array( $key => $value ) ) );
} else {
return str_replace( array( 'a:1:{i:0;', '}' ), array( '', '' ), serialize( array( $value ) ) );
}
}
Here is an example usage:
$users = get_users_by_business_category( '-Entrepreneurship' );
So what happens here is that we serialize an array that contains our search value - in this case "-Entrepreneurship" and we strip out the beginning and end of the serialized definition of the array. So the 'value'
part of our meta_query
would be this:
s:17:"-Entrepreneurship";
Setting compare
to LIKE
will make WordPress add %
before and after our search value, so it would actually look like this in the final query: '%s:17:"-Entrepreneurship";%'
and so it would match any records that contain "-Entrepreneurship" in them.
Upvotes: 5
Reputation: 1450
New fields inside user profile will be saved as meta data. So you need to query using meta_key and meta_value.
Try this,
function get_user_by_meta_data( $meta_key, $meta_value ) {
// Query for users based on the meta data
$user_query = new WP_User_Query(
array(
'meta_key' => $meta_key,
'meta_value' => $meta_value
)
);
// Get the results from the query, returning the first user
$users = $user_query->get_results();
if($users){
return $users[0];
}else{
return false;
}
} // end get_user_by_meta_data
$getResult = get_user_by_meta_data('business_category', 1); // Example
I hope this helps.
Upvotes: 0