Skip Navigation

[Resolved] WP_User_Query can’t find custom user field

This thread is resolved. Here is a description of the problem and solution.

Problem:

How to search in custom checkboxes using PHP codes.

Solution:

See the example codes here:

https://toolset.com/forums/topic/wp_user_query-cant-find-custom-user-field/#post-1725335

Relevant Documentation:

https://codex.wordpress.org/Class_Reference/WP_User_Query

This support ticket is created 4 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by WeiS2074 4 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#1723443
user2.PNG
user1.PNG

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?

#1723569
checkbox.JPG

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

#1723641

I can use a single checkbox, but it isn't a good design. How can I find out the value of serialized array?

#1724993

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 );

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/can-i-query-user-emails-and-custom-fields-at-the-same-time/

#1725335

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/

#1725339

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?

#1725343

For custom checkboxes field, the 'compare' => 'LIKE' is required, I have tested above codes in my localhost, it works fine.

#1725345

I tested it too. it works. Thank you!