Skip to content

Instantly share code, notes, and snippets.

@denisbaranov
Last active February 12, 2023 17:59
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 denisbaranov/3631abb45a05d6cdfbb6a4b6a6862b4c to your computer and use it in GitHub Desktop.
Save denisbaranov/3631abb45a05d6cdfbb6a4b6a6862b4c to your computer and use it in GitHub Desktop.
This example shows how to group several filters in the Members Directory of the Ultimate Member.
<?php
/**
* This example shows how to group several filters in the Members Directory of the Ultimate Member.
* For example there are three phone fields in the profile ("Phone", "Phone number", "Mobile Number"),
* but you want to use a single filter "Phone number" to filter by these three fields in the Member Directory.
*
* Note: This example works if the setting "Enable custom table for usermeta" is turned off.
* See the gist https://gist.github.com/denisbaranov/dd8e7c0845af35eeba2bdd08f5f72a76 otherwise.
*
* Change the variable $um_grouped_filtes. Add custom code to the file functions.php in the active theme directory.
*
* Ultimate Member documentation: https://docs.ultimatemember.com
*/
add_filter( 'um_prepare_user_query_args', function ( $query_args, $directory_data_settings ) {
global $um_grouped_filtes;
// Add your grouped filters here in a format 'main_filter' => [ 'additional_filter' ]
$um_grouped_filtes = apply_filters( 'um_directory_grouped_filtes', array(
'country' => array( 'billing_country', 'shipping_country' ),
'phone_number' => array( 'mobile_number', 'phone' )
) );
if ( !function_exists( 'um_directory_meta_query__grouped_filtes' ) ) {
function um_directory_meta_query__grouped_filtes( $filter ) {
global $um_grouped_filtes;
if ( is_array( $filter ) && isset( $filter['key'] ) && isset( $um_grouped_filtes[$filter['key']] ) ) {
$grouped_meta_query = array(
'relation' => 'OR',
"{$filter['key']}" => $filter
);
foreach ( $um_grouped_filtes[$filter['key']] as $metakey ) {
$grouped_meta_query[$metakey] = array_merge( $filter, array( 'key' => $metakey ) );
}
$filter = $grouped_meta_query;
} elseif ( is_array( $filter ) && !isset( $filter['key'] ) ) {
$filter = array_map( 'um_directory_meta_query__grouped_filtes', $filter );
}
return $filter;
}
}
if ( isset( $query_args['meta_query'] ) && is_array( $query_args['meta_query'] ) ) {
$query_args['meta_query'] = array_map( 'um_directory_meta_query__grouped_filtes', $query_args['meta_query'] );
}
return $query_args;
}, 20, 2 );
@yuriinalivaiko
Copy link

This gist is a part of the article Use a single control to filter by several fields in the members directory

Note: This solution is not recommended because it significantly slows down the directory. The relationship between the number of combined fields and slowdown is power-law: combining two fields slows down the search by two times, combining three fields - four times, four fields - eight times, and so on. It's better to use a general search line that searches by all fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment