• Resolved artmerk

    (@artmerk)


    I want to exclude a certain user role when doing sync.

    I used this exact code (only changing the name of my role to exclude):
    https://github.com/ibericode/mc4wp-snippets/blob/master/add-ons/user-sync/exclude-roles.php

    add_filter( 'mailchimp_sync_should_sync_user', function( $sync, $user ) {
        // setup array of roles which should be excluded from Syncing.
        $excluded_roles = array(
          'Inactive',
          'some_other_role'
        );
        // check each role
        foreach( $excluded_roles as $excluded_role ) {
          // do not synchronize user if it has the excluded role.
          if( in_array( $excluded_role, $user->roles ) ) {
            return false;
          }
        }
        // use default value (from settings)
        return $sync;
    }, 10, 2);
    

    And it does not work, meaning, if a user has the above role(s), it is still synced. We do NOT want to synchronize user if it has the excluded role.

    I could also exclude based on a meta_value if that would be easier??

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter artmerk

    (@artmerk)

    I found / fixed my problem: my role had been renamed, but only the slug had changed. So the role name used in the function did not match, therefore no roles were excluded. So the code above does work.

    But — has anyone successfully written code to exclude sync based on user meta value?? something like:

    $excluded_meta = new WP_User_Query( array( ‘meta_key’ => ‘account_status’, ‘meta_value’ => ‘inactive’ ) );

    • This reply was modified 7 years, 3 months ago by artmerk.
    Plugin Contributor Lap

    (@lapzor)

    Hi, thanks for letting us know you got it fixed, I’m glad to hear our GitHub code is still working as intended.

    You can write your own code / conditions and return false to not sync contacts with those conditions. I’m afraid I can’t write the code for you, as that would be far outside the scope of our free support.

    add_filter( ‘mailchimp_sync_should_sync_user’, function( $sync, $user ) {
    if( YOUR OWN CONDITION HERE ) {
    return false;
    }
    return $sync;
    }, 10, 2);

    You can dump the $user and to see what you have to work with.

    Hope that helps,

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sync and Exclude on User Role – github code NOT working’ is closed to new replies.