Last active
February 12, 2023 17:59
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
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.