Skip to content

Instantly share code, notes, and snippets.

@denisbaranov
Last active February 12, 2023 17:59
Show Gist options
  • Save denisbaranov/dd8e7c0845af35eeba2bdd08f5f72a76 to your computer and use it in GitHub Desktop.
Save denisbaranov/dd8e7c0845af35eeba2bdd08f5f72a76 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 (with the custom table for usermeta).
<?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 on.
* See the gist https://gist.github.com/denisbaranov/3631abb45a05d6cdfbb6a4b6a6862b4c 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_modify_sortby_parameter_meta', function( $sql_order ) {
global $um_grouped_filtes, $wpdb;
// 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' )
) );
$md = UM()->member_directory();
$directory_id = $md->get_directory_by_hash( $_POST['directory_id'] );
$directory_data = UM()->query()->post_data( $directory_id );
// filters
if ( !empty( $md->custom_filters_in_query ) ) {
$i = count( $md->custom_filters_in_query );
foreach ( $md->custom_filters_in_query as $fk => $value ) {
if ( empty( $um_grouped_filtes[$fk] ) ) {
continue;
}
$grouped_keys = array_merge( $um_grouped_filtes[$fk], array( $fk ) );
// JOIN
foreach ( $um_grouped_filtes[$fk] as $metakey ) {
$i++;
$md->handle_filter_query( $directory_data, $metakey, $value, $i );
}
// WHERE
$where = array();
foreach ( $md->where_clauses as $j => $clause ) {
foreach ( $grouped_keys as $key ) {
if ( strpos( $clause, $key ) ) {
$where[] = $clause;
unset( $md->where_clauses[$j] );
}
}
}
$md->where_clauses[] = '((' . implode( ') OR (', $where ) . '))';
}
}
return $sql_order;
}, 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