Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Mike Walsh

    (@mpwalsh8)

    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 for details and example filter usage.

    Here is another post which has some more information.

    Thread Starter pandreas

    (@pandreas)

    Ok, thank you for your quick reply, I appreciate it very much!

    I have also discovered this post so, I will try these solutions..

    Thank you again!

    Thread Starter pandreas

    (@pandreas)

    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

    (@mpwalsh8)

    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

    (@pandreas)

    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

    (@mpwalsh8)

    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

    (@pandreas)

    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.