Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ideadude/8990b90b1df00f1e655b117b63e049f3 to your computer and use it in GitHub Desktop.
Save ideadude/8990b90b1df00f1e655b117b63e049f3 to your computer and use it in GitHub Desktop.
Allow meta filtering of the PMPro membership directory
/**
* Allow meta filtering of the membership directory
* if pk and ps params are passed into the URL.
* PMPro and the PMPro Member Directory Add Ons should be active.
* Then add this code to a custom plugin.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_add_meta_queries_to_pmpro_directory_search( $sql_parts ) {
global $wpdb;
// get the meta key and search terms
if(isset($_REQUEST['ps'])) {
$s = sanitize_text_field( $_REQUEST['ps'] );
} else {
$s = "";
}
if(isset($_REQUEST['pk'])) {
$key = sanitize_text_field( $_REQUEST['pk'] );
} else {
$key = "";
}
// add meta filtering if passed in
if ( !empty( $s ) && !empty( $key ) ) {
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta umk ON umk.meta_key = '" . esc_sql( $key ) . "' AND umk.user_id = u.ID ";
$sql_parts['WHERE'] .= " AND umk.meta_value LIKE '%" . esc_sql( $s ) . "%' ";
}
return $sql_parts;
}
add_filter( 'pmpro_member_directory_sql_parts', 'my_add_meta_queries_to_pmpro_directory_search' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment