Title: filter users with specific profile field
Last modified: August 31, 2016

---

# filter users with specific profile field

 *  [pandreas](https://wordpress.org/support/users/pandreas/)
 * (@pandreas)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/filter-users-with-specific-profile-field/)
 * I want to send email to users having a specific value to a profile field.
 * Can this be done?
 * Thank you!
 * Andreas
 * [https://wordpress.org/plugins/email-users/](https://wordpress.org/plugins/email-users/)

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Plugin Author [Mike Walsh](https://wordpress.org/support/users/mpwalsh8/)
 * (@mpwalsh8)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/filter-users-with-specific-profile-field/#post-7001336)
 * Yes it can. I have an example on my web site where I define a couple of groups(
   Fire Department, Police Department) and show how custom meta filters can be used
   to target specific users.
 * See [this post](http://michaelwalsh.org/blog/2013/02/email-users-4-4-1-beta-2-available/)
   for details and example filter usage.
 * Here is [another post](http://michaelwalsh.org/blog/2013/02/email-users-4-4-1-beta-1-available/)
   which has some more information.
 *  Thread Starter [pandreas](https://wordpress.org/support/users/pandreas/)
 * (@pandreas)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/filter-users-with-specific-profile-field/#post-7001371)
 * Ok, thank you for your quick reply, I appreciate it very much!
 * I have also discovered [this post](https://wordpress.org/support/topic/email-to-user-meta-field?replies=33)
   so, I will try these solutions..
 * Thank you again!
 *  Thread Starter [pandreas](https://wordpress.org/support/users/pandreas/)
 * (@pandreas)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/filter-users-with-specific-profile-field/#post-7001431)
 * I have checked it and works great.
 * But, I use buddypress xprofile for extra profile meta data.
 * How can I filter users based on a custom field of xprofile buddypress?
 * I have to tell you that these xprofile meta values are written at table ‘bp_xprofile_fields’
   and ‘bp_xprofile_data’ of database and not at table ‘usermeta’.
 * Thank you!
 * Andreas
 *  Plugin Author [Mike Walsh](https://wordpress.org/support/users/mpwalsh8/)
 * (@mpwalsh8)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/filter-users-with-specific-profile-field/#post-7001439)
 * The easiest way would be to have some code which periodically copies the BuddyPress
   meta data to standard WordPress user meta data.
 * How often this copy happens would be up to you to decide. If you don’t have too
   many users you could do it each time the Email Users filter is invoked. If you
   have a lot of users you might want to make it a scheduled process with wp_cron.
 *  Thread Starter [pandreas](https://wordpress.org/support/users/pandreas/)
 * (@pandreas)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/filter-users-with-specific-profile-field/#post-7001442)
 * Ok got it, thank you!
 * But how can I add new user meta data without the use of xprofile?
 * I mean, I only have to use update_user_meta function to update WordPress standard
   user profile?
 * Thank you.
 * Andreas
 *  Plugin Author [Mike Walsh](https://wordpress.org/support/users/mpwalsh8/)
 * (@mpwalsh8)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/filter-users-with-specific-profile-field/#post-7001459)
 * I have zero experience with BuddyPress so I may not be the best person to ask
   but what I was thinking was something like this:
    1. Use Buddy Press “bp_profile” fields as you do now. No change here.
    2. At some regularly scheduled interval (schedule with wp_cron?) have some sort
       of a custom function (could be your own plugin, could be added to your theme’s
       functions.php file) which does nothing more than copy the BuddyPress fields 
       of interest to the appropriate user’s user meta fields. You’d need to define
       some sort of mapping but I would probably just use the same BP field names and
       values.
    3. Use the copied user meta data to drive the EMail Users user meta filter process.
 * Steps 2 and 3 require some development. I don’t think it is hard and I believe
   someone, or possibly multiple people, have done something similar to solve this
   problem previously.
 * Will it work? Yes. It is simple to do? No, it requires writing some code.
 *  Thread Starter [pandreas](https://wordpress.org/support/users/pandreas/)
 * (@pandreas)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/filter-users-with-specific-profile-field/#post-7001495)
 * Ok, I did it!
 * I wrote the following simple code – my custom field is country:
 *     ```
       function xprofile_sync_extra_field_wp_profile( $user_id = 0 ) {
   
       	// Bail if profile syncing is disabled.
       	if ( bp_disable_profile_sync() ) {
       		return true;
       	}
   
       	if ( empty( $user_id ) ) {
       		$user_id = bp_loggedin_user_id();
       	}
   
       	if ( empty( $user_id ) ) {
       		return false;
       	}
   
               //Get country info from xprofile
               $country = xprofile_get_field_data('Country', $user_id);
   
               if (!get_user_meta($user_id, 'country'))
               {
                   add_user_meta($user_id, 'country', $country);
               } else
                   bp_update_user_meta( $user_id, 'country',  $country  );
       }
       add_action( 'xprofile_updated_profile', 'xprofile_sync_extra_field_wp_profile' );
       add_action( 'bp_core_signup_user',      'xprofile_sync_extra_field_wp_profile' );
       add_action( 'bp_core_activated_user',   'xprofile_sync_extra_field_wp_profile' );
       ```
   
 * So, every time there is a new user, or a user is activated or the profile is 
   updated, the custom field is being updated (or added) at the wp_usermeta table.
 * And I run first once the following in order to fill in key-value.
 *     ```
       $users = get_users( array( 'fields' => array( 'id' ) ) );
               foreach ($users as $user) {
                   $memberid = $user->id;
                   $country = xprofile_get_field_data('Country', $memberid);
                   if ($country) {
                       add_user_meta($memberid, 'country', $country);
                   //var_dump($country); die('kiki');
                   }
               }
       ```
   
 * It works now very well!
 * Thank you for your support!

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘filter users with specific profile field’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/email-users_a0e0cf.svg)
 * [Email Users](https://wordpress.org/plugins/email-users/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/email-users/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/email-users/)
 * [Active Topics](https://wordpress.org/support/plugin/email-users/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/email-users/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/email-users/reviews/)

## Tags

 * [email](https://wordpress.org/support/topic-tag/email/)
 * [filter](https://wordpress.org/support/topic-tag/filter/)

 * 7 replies
 * 2 participants
 * Last reply from: [pandreas](https://wordpress.org/support/users/pandreas/)
 * Last activity: [10 years, 6 months ago](https://wordpress.org/support/topic/filter-users-with-specific-profile-field/#post-7001495)
 * Status: not resolved