Skip to content

Instantly share code, notes, and snippets.

@RadGH
Last active May 23, 2020 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RadGH/877ee20d99eee73fb0e933f199d4b8fb to your computer and use it in GitHub Desktop.
Save RadGH/877ee20d99eee73fb0e933f199d4b8fb to your computer and use it in GitHub Desktop.
Remove duplicate users from WP_User_Query
<?php
/**
* Fix duplicate users appearing in WP_User_Query by specifying an OR meta query.
* This solution makes no sense but it works. ¯\_(ツ)_/¯
*
* See:
* 1. https://gist.github.com/RadGH/877ee20d99eee73fb0e933f199d4b8fb
* 2: https://wordpress.stackexchange.com/questions/220307/user-appears-twice-in-a-wp-user-query
* 3: https://core.trac.wordpress.org/ticket/17582
*
* @param WP_User_Query $query
*/
function fix_duplicate_user_query_99d4b8fb( $query ) {
if ( !empty($query->get('meta_query')) ) {
$query->set( 'meta_query', array( 'relation' => 'OR', $query->get('meta_query') ) );
}
}
add_action( 'pre_get_users', 'fix_duplicate_user_query_99d4b8fb', 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment