Skip Navigation

[Resolved] need to query a user view based on email

This support ticket is created 4 years, 9 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 4 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1490667

Tell us what you are trying to do?

i need to display a group of users based of of there email domain. is this possible?

I going to do a query filter but there is no option to filter by user email.

I have a companies CPT and i am trying to associate all the companies employees to the company, ie: display all there employees based off there user email.

#1490991

Hi, there's not an easy way to do this in wp-admin alone, but one of our Views filters will allow you to programmatically manipulate the User Query with a bit of custom PHP. First, you would create a View of Users without any Query Filters. Then you would include the following example code in a custom code snippet. This snippet will find all users whose emails include the string "domain.com":

add_filter( 'wpv_filter_user_query', 'tssupp_filter_users_by_email_domain', 99, 3 );

function tssupp_filter_users_by_email_domain( $query_args, $view_settings, $view_id ) {
  $views = array( 12345 );
  if ( in_array( $view_id, $views ) )
  {
    $query_args['search'] = "*domain.com*";
    $query_args['search_columns'] = array( 'user_email' );
  }
  return $query_args;
}

You would replace 12345 with the numeric ID of this View of Users, and replace domain.com with the desired email domain. Then place this View on a custom Page or post or template and check the front-end results.

Documentation for our filter: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_user_query
Documentation for WP_User_Query: https://developer.wordpress.org/reference/classes/WP_User_Query/prepare_query/

#1494613

thats awesome thanks.

one more thing. so this users view would sit on each companies CPT page, obviously each company has a different email domain, so how can I pass the email domain into the custom function dynamically so I dont have to make a billion different views and functions?

#1494791

You can pass a domain into the View shortcode using shortcode attributes:

[wpv-view name="Your Users Filtered by Email View" domain="gmail.com"]

Then access that shortcode attribute in the filter like so:

add_filter( 'wpv_filter_user_query', 'tssupp_filter_users_by_email_domain', 99, 3 );

function tssupp_filter_users_by_email_domain( $query_args, $view_settings, $view_id ) {
  global $WP_Views;
  $views = array( 12345 );
  if ( in_array( $view_id, $views ) )
  {
    $shortcode_atts = $WP_Views->view_shortcode_attributes[0];
    $domain = $shortcode_atts['domain'];
    $query_args['search'] = "*" . $domain . "*";
    $query_args['search_columns'] = array( 'user_email' );
  }
  return $query_args;
}