Created
May 13, 2020 19:05
-
-
Save ipokkel/cd204e38853aac1c072a4ac2f559e3f9 to your computer and use it in GitHub Desktop.
Custom SQL query for PMPro Directory Widget that also filters field values stored as arrays
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 | |
function my_pmpro_directory_widget_filter_sql_parts( $sql_parts, $levels, $s, $pn, $limit, $start, $end, $order_by, $order ) { | |
global $wpdb; | |
// Filter results based on ares of practice is selected. | |
if ( ! empty( $_REQUEST['areasofpractice'] ) && is_array( $_REQUEST['areasofpractice'] ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_areasofpractice ON um_areasofpractice.meta_key = 'areasofpractice' AND u.ID = um_areasofpractice.user_id "; | |
$sql_parts['WHERE'] .= ' AND ( '; | |
$first_areasofpractice = true; | |
foreach ( $_REQUEST['areasofpractice'] as $areasofpractice ) { | |
if ( $first_areasofpractice ) { | |
$first_areasofpractice = false; | |
} else { | |
$sql_parts['WHERE'] .= ' OR '; | |
} | |
$sql_parts['WHERE'] .= " um_areasofpractice.meta_value like '%{$areasofpractice}%' "; | |
} | |
$sql_parts['WHERE'] .= ' ) '; | |
} | |
return $sql_parts; | |
} | |
add_filter( 'pmpro_member_directory_sql_parts', 'my_pmpro_directory_widget_filter_sql_parts', 10, 9 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This recipe is included in the blog post on "Allow Users to Filter Results in your Member Directory" at Paid Memberships Pro here: https://www.paidmembershipspro.com/allow-users-to-filter-results-in-your-member-directory/