Default Directory filter

BP Profile Search › Default Directory filter

  • Author
    Post
  • #11635 Reply
    Anderson
    Guest

    Hi 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
    Anderson

    #11636 Reply
    andrea
    Plugin Author

    Hi 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.

    #11637 Reply
    Anderson
    Guest

    Thanks 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
    Anderson

    #11638 Reply
    andrea
    Plugin Author

    Hi 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?

    #11639 Reply
    Anderson
    Guest

    Hi 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

    #11640 Reply
    andrea
    Plugin Author

    Hi 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.

    #11652 Reply
    Anderson
    Guest

    Thanks 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
    Anderson

    #11653 Reply
    Anderson
    Guest

    After further investigation it seems to remove the capsule_tags from the usermeta dropdown in the Edit Form sections.

    #11654 Reply
    andrea
    Plugin Author

    Hi 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?

    #11655 Reply
    #11656 Reply
    andrea
    Plugin Author

    But 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.

Reply to: Default Directory filter
My Information