Created
May 4, 2023 05:12
-
-
Save ipokkel/e9fbd7ae7ae8ed5df4974f3656ab6889 to your computer and use it in GitHub Desktop.
Filter the PMPro Member Directory results by user meta value. This can be used to only display members that completed a user field with a specific value.
This file contains hidden or 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 | |
/** | |
* Filter PMPro Member Directory 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; | |
$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