Skip Navigation

[Resolved] Can I query user emails and custom fields at the same time?

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

Problem:
Can I query user emails and custom fields at the same time with PHP codes?

Solution:

That is possible with WP_User_Query, for example:

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

Relevant Documentation:

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

This support ticket is created 3 years, 7 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 5 replies, has 2 voices.

Last updated by WeiS2074 3 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#1725333

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

#1725341

Hello,

According to WP document:
https://codex.wordpress.org/Class_Reference/WP_User_Query#Search_Parameters

The "search" supports only one string value, does not support array value:

search (string) - Searches for possible string matches on columns.

So you can only search the result using one email address, for example:

	$args = array (
		'meta_query' => array(
			array(
				'key' => 'wpcf-cf-user-red-grape-varieties',
				'value' => 'Shiraz1',
			   'compare' => 'LIKE'
			)
		),
		'search' => 'a@a.com' ,
		'search_columns' => array( 'user_email' )
	);
	$wp_user_query = new WP_User_Query($args);
	$authors = $wp_user_query->get_results();

...

#1725347

How about key and value in meta_query? Can they be an array?

$args = array (
'meta_query' => array(
array(
'key' =>array("wpcf-cf-user-red-grape-varieties", "wpcf-cf-user-white-grape-varieties") ,
'value' =>array("Shiraz", "merlot")
'compare' => 'LIKE'
)
),
'search' => 'a@a.com' ,
'search_columns' => array( 'user_email' )
);
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();

#1725349

That is possible with WP_User_Query, for example:

	$args = array (
		'meta_query' => array(
			array(
				'key' => 'wpcf-cf-user-red-grape-varieties',
				'value' => 'Shiraz1',
			   'compare' => 'LIKE'
			),
			array(
				'key' => 'wpcf-cf-user-red-grape-varieties',
				'value' => 'merlot',
			   'compare' => 'LIKE'
			),
			'relation' => 'OR',
		),
		'search' => 'a@a.com' ,
		'search_columns' => array( 'user_email' )
	);
	$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#Custom_Field_Parameters

See the example codes in above document: "Multiple custom user fields handling"

#1725539

please ignore this message

#1725595

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.