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.
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/
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?
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;
}