Title: Sync and Exclude on User Role &#8211; github code NOT working
Last modified: August 25, 2017

---

# Sync and Exclude on User Role – github code NOT working

 *  Resolved [artmerk](https://wordpress.org/support/users/artmerk/)
 * (@artmerk)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/sync-and-exclude-on-user-role-github-code-not-working/)
 * 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](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](https://wordpress.org/support/users/artmerk/)
 * (@artmerk)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/sync-and-exclude-on-user-role-github-code-not-working/#post-9442064)
 * 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 8 years, 9 months ago by [artmerk](https://wordpress.org/support/users/artmerk/).
 *  Plugin Contributor [Lap](https://wordpress.org/support/users/lapzor/)
 * (@lapzor)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/sync-and-exclude-on-user-role-github-code-not-working/#post-9444525)
 * 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.

 * ![](https://s.w.org/plugins/geopattern-icon/mailchimp-sync_cfeefa.svg)
 * [MC4WP: Mailchimp User Sync](https://wordpress.org/plugins/mailchimp-sync/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/mailchimp-sync/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/mailchimp-sync/)
 * [Active Topics](https://wordpress.org/support/plugin/mailchimp-sync/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/mailchimp-sync/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/mailchimp-sync/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [Lap](https://wordpress.org/support/users/lapzor/)
 * Last activity: [8 years, 9 months ago](https://wordpress.org/support/topic/sync-and-exclude-on-user-role-github-code-not-working/#post-9444525)
 * Status: resolved