Hi,
I have a user filed, one user tick one of the option in this filed. I try to query it by using below code, it doesn't work.
$args = array (
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'wpcf-cf-user-red-grape-varieties',
'value' => 'Shiraz'
),
array(
'key' => 'cf-user-red-grape-varieties',
'value' => 'Shiraz'
)
)
);
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
If I change the query option into
array(
'key' => 'first_name',
'value' =>'w'
)
then it can find the user. what's wrong with my query?
Hello,
According to your screenshot you are using a custom checkboxes field "cf-user-red-grape-varieties", the custom checkboxes field is for multiple choices options, and stores value in serialized array, so your PHP codes won't work.
In your case, you just need only one option, so I suggest you try with custom checkbox field, which stores value in plain text format.
For example:
1) Create a custom checkbox field "cf-user-red-grape-varieties2", see screenshot: checkbox.JPG
2) Edit each user, setup value in field "cf-user-red-grape-varieties2"
3) then your PHP codes should be able to work, for example:
$args = array (
'meta_query' => array(
array(
'key' => 'wpcf-cf-user-red-grape-varieties2',
'value' => 'Shiraz'
)
)
);
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
More help:
https://codex.wordpress.org/Class_Reference/WP_User_Query
I can use a single checkbox, but it isn't a good design. How can I find out the value of serialized array?
Can I query user emails and custom fields at the same time? If I use SQL, it would be same as "Joint"
select *
from tblUser u
join tblCustomField cf where u.userid = cf.userid
Is below query correct?
$subscribed_emails = array("a@a.com", "b@b.com");
$args1 = array (
'search' => $subscribed_emails ,
'search_columns' => array( 'user_email' )
);
$args = array (
'meta_query' => array(
array(
'key' => 'wpcf-cf-user-red-grape-varieties2',
'value' => 'Shiraz'
)
)
);
$query1 = new WP_User_Query( $args1 );
$query2 = new WP_User_Query( $args2 );
$wp_user_query = new WP_User_Query();
$wp_user_query->results = array_merge( $query1->results,$query2->results );
For the original question, I assume you insist on checkboxes field.
If it is, you can modify the PHP codes as below:
$args = array (
'meta_query' => array(
array(
'key' => 'wpcf-cf-user-red-grape-varieties',
'value' => 'Shiraz',
'compare' => 'LIKE'
)
)
);
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
According to our support policy, we prefer to one ticket one question, for other new questions, please check the new thread here:
https://toolset.com/forums/topic/can-i-query-user-emails-and-custom-fields-at-the-same-time/
In the last reply, you said the option value is a serialized array, but what your code changed is just add 'compare' => 'LIKE'. Is "Compare" a mandatory field? if Yes, why some times I use WP_User_Query without "compare" and it works?
For custom checkboxes field, the 'compare' => 'LIKE' is required, I have tested above codes in my localhost, it works fine.
I tested it too. it works. Thank you!