Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment