Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/8d3101bed445982b9e453577d2b00d21 to your computer and use it in GitHub Desktop.
Save dwanjuki/8d3101bed445982b9e453577d2b00d21 to your computer and use it in GitHub Desktop.
Filter a certain PMPro Member Directory page results by user meta value
<?php
/**
* Filter a specific PMPro Member Directory page by custom user meta field.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_directory_filter_based_on_user_meta( $sql_parts, $levels, $s, $pn, $limit, $start, $end, $order_by, $order ) {
global $wpdb, $post;
if( $post->ID !== 123 ) { // replace 123 with the Post ID of the directory page to filter
return $sql_parts;
}
$meta_key = 'meta_key'; // Change 'meta_key' to the name of your user field.
$meta_value = 'meta_value'; // Change 'meta_value' to the value of your user field.
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_field_1 ON um_field_1.meta_key = '" . esc_sql( $meta_key ) . "' AND um_field_1.user_id = u.ID ";
$sql_parts['WHERE'] .= " AND um_field_1.meta_value = '" . esc_sql( $meta_value ) . "' ";
return $sql_parts;
}
add_filter( 'pmpro_member_directory_sql_parts', 'my_pmpro_directory_filter_based_on_user_meta', 10, 9 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment