Filter out users if value is null

Home Forums BP Profile Search Forum Filter out users if value is null

  • This topic has 5 replies, 1 voice, and was last updated 1 month ago by James.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #10647 Reply
    James
    Guest

    Hi,

    I’d like to set up a form + associated directory that only returns results where a particular field has any value.

    e.g. For directory “A”, only show results where value 1 is not null, but still have the ability for directory “B” to show results from all users.

    I suspect that my_hidden_filters function would be helpful, but not sure of the syntax from the examples in the forum.

    #10648 Reply
    James
    Guest

    I think I figured it out the is not null part with the code below, but not sure on how to only apply this filter to a single directory.

    Any help appreciated!

    add_filter ('bps_hidden_filters', 'my_hidden_filters');
    function my_hidden_filters ($filters)
    {
    	$user_id = bp_loggedin_user_id ();
    	$f2 = xprofile_get_field_data (2, $user_id);
    	if ($user_id != 0 && !is_null($f2)) {
    		$filters['field_2'] = $f2;
    }
    	return $filters;
    }
    #10649 Reply
    andrea
    Keymaster

    Hi James,

    To limit your hidden filters to a specific directory, you can use this code:

    add_filter ('bps_hidden_filters', 'my_hidden_filters');
    function my_hidden_filters ($filters)
    {
        if (!is_page('Page Title'))  return $filters;
    
        // add your hidden filters for 'Page Title' here
    
        return $filters;
    }

    Replace ‘Page Title’ with the actual title of the page where you put the [bps_directory] shortcode.

    By the way, the code you posted adds the filter only if the current user has a non-empty value in his/her own profile. Is that what you need?

    #10650 Reply
    James
    Guest

    hey, thanks

    and, no – I had hoped for the directory to only return results for all users with non-null values.

    #10651 Reply
    andrea
    Keymaster

    If your field type is ‘Text Box’, you can show all the members whose field value is not empty with this shortcode:

    [bps_directory field_37_like=%]

    Does that work for you?

    #10661 Reply
    James
    Guest

    Hi,

    Yes that worked, thanks!

Viewing 6 posts - 1 through 6 (of 6 total)
Reply To: Filter out users if value is null
Your information: