BP Profile Search › Default Directory filter
-
AuthorPost
-
Anderson
GuestHi Andrea,
Is it possible to filter users in the directory using a usermeta key?
I’ve added a usermeta key using the add_more_keys filter and now I want to filter the users based on whether the new field contains the value of another XProfile field.
Thanks
Andersonandrea
Plugin AuthorHi Anderson,
If your usermeta key is “mykey” and you only wish to display users with their “mykey” value equal to “whatever”, you can do so using the shortcode:
[bps_directory mykey="whatever"]If, instead of a fixed value, you wish to use the value of a field from the current user’s profile, you can add the following code to your bp-custom.php:
add_filter ('bps_hidden_filters', 'my_hidden_filters'); function my_hidden_filters ($filters) { $user_id = bp_loggedin_user_id (); if ($user_id != 0) { $value = xprofile_get_field_data (27, $user_id); } $filters['mykey'] = empty ($value)? 'none': $value; return $filters; }Replace 27 with your actual field ID, and ‘mykey’ with your actual usermeta key.
Anderson
GuestThanks Andrea,
In the meantime I created a function to dynamically add the field value to the shortcode which seems to work but I definitely prefer your method.
I need to update it so that it uses the CONTAINS method. Is that still possible with your code? The mykey CONTAINS the value from xprofile_get_field_data (27, $user_id) as it’s an array of values.
Cheers
Andersonandrea
Plugin AuthorHi Anderson,
A few questions:
1. Is mykey an array of values, and field_27 a single value?
2. What code are you using to add the usermeta mykey to BP Profile Search?
3. What type is the field_27 profile field?
Anderson
GuestHi Andrea,
1. Is mykey an array of values, and field_27 a single value?
Yes.
The array might look like:
[capsule_tags] => Array
(
[0] => 3872434
[1] => 3978143
[2] => 4472700
[3] => 4622690
[4] => 4647735
[5] => 4643262
)and field 27 might be 4622690
2. What code are you using to add the usermeta mykey to BP Profile Search?
add_action (‘bps_usermeta_keys’, ‘add_more_keys’);
function add_more_keys ($keys)
{$keys[‘capsule_tags’] = array (‘text’);
return $keys;
}3. What type is the field_27 profile field?
Text but it can be a number if it needs to change.
Thanks for your assistance with this.
Anderson
andrea
Plugin AuthorHi Anderson,
You can try this:
a) modify your bps_usermeta_keys filter as follows:
add_action ('bps_usermeta_keys', 'add_more_keys'); function add_more_keys ($keys) { $keys['capsule_tags'] = array ('set'); // line changed return $keys; }b) modify your bps_hidden_filters as follows:
add_filter ('bps_hidden_filters', 'my_hidden_filters'); function my_hidden_filters ($filters) { $user_id = bp_loggedin_user_id (); if ($user_id != 0) { $value = xprofile_get_field_data (27, $user_id); } $filters['capsule_tags_match_any'] = empty ($value)? 'none': $value; // line changed return $filters; }Now your Members directory should only display users whose capsule_tags usermeta array contains the value specified in the current user’s field_27.
Anderson
GuestThanks Andrea,
I gave that a go but it still seems to return all the users and not the filtered ones. Is there anything that could override the filter?
Thanks
AndersonAnderson
GuestAfter further investigation it seems to remove the capsule_tags from the usermeta dropdown in the Edit Form sections.
andrea
Plugin AuthorHi Anderson,
I think we are close to the solution, but now I have to test my code. Which plugin are you using to create the capsule_tags usermeta?
Anderson
GuestSounds promising!
It’s coming from wp-fusion. It’s syncing the data from an external crm to the usermeta info.More info:
https://wpfusion.com/documentation/filters/wpf_pulled_user_meta/
https://wpfusion.com/documentation/advanced-developer-tutorials/detecting-and-syncing-additional-fields/
https://wpfusion.com/documentation/tutorials/batch-operations/#pull-user-metaandrea
Plugin AuthorBut that means I can’t set up my test site without the external CRM.
Maybe I can use your test site to debug my code? If that’s OK with you, you can use my Contact page to privately share a WP admin access.
-
AuthorPost