1. Customize
  2. Leaderboards
  3. Filter users by a specific meta

Filter users by a specific meta

The following snippet shows how to filter users on a leaderboard based on a specific meta value.

If you don’t know exactly the name of a meta key, you can use a plugin like JSM’s Show User Meta to see all stored user meta keys.

function my_prefix_filter_leaderboard_users_by_meta( $query_vars, $leaderboard_id, $leaderboard ) {

    global $wpdb;

    $meta_key = 'your_meta_key';        // Set the meta key you want to filter
    $meta_value = 'Your meta value';    // Set the meta value you want to filter
    $compare = '=';                     // Accepted values: =, !=, >, <, >=, <=

    $query_vars['join'][] = "LEFT JOIN {$wpdb->usermeta} AS umeta ON ( umeta.user_id = u.ID )";
    $query_vars['where'][] = "(  umeta.meta_key = '{$meta_key}' AND umeta.meta_value {$compare} '{$meta_value}' )";

    return $query_vars;

}
add_filter( 'gamipress_leaderboards_leaderboard_pre_query_vars', 'my_prefix_filter_leaderboard_users_by_meta', 10, 3 );
Was this article helpful to you? Yes No

How can we help?