Skip to:
Content
Pages
Categories
Search
Top
Bottom

Hide some users from members directory


  • amkh
    Participant

    @amkh

    Is it possible to hide some users from members directory? These members should be able to see the members directory but they themselves should be hidden / not displayed in the members directory.

Viewing 17 replies - 1 through 17 (of 17 total)

  • Prashant Singh
    Participant

    @prashantvatsh

    Hi

    Please try this code:

    function ps_exclude_current_user( $qs = false, $object = false ) {
    
     if( !is_user_logged_in() )
         return $qs;
     $user_ids = array('1','2');
     $excluded_user=  join(',', $user_ids);
      
     if( $object !='members' )
    	return $qs;
      
     $args=wp_parse_args($qs);
      
     if( !empty( $args[ 'user_id' ] ) )
    	return $qs;
      
     if( !empty( $args['exclude'] ) )
    	$args['exclude'] = $args['exclude'] .','. $excluded_user;
     else
    	$args['exclude'] = $excluded_user;
      
    	$qs = build_query( $args );
      
     return $qs;
      
    }
    add_action('bp_ajax_querystring','ps_exclude_current_user', 21, 2 );

    In place of ‘1’, ‘2’ you have to write your user’s ids and you can add as many of them there like, I have added two for example.

    Hopefully, this will help you.

    Thanks


    amkh
    Participant

    @amkh

    Thank you very much. I added the snippet mentioned in the snippets plugin. However the profiles are still seen in membership directory.

    function ps_exclude_current_user( $qs = false, $object = false ) {
    
     if( !is_user_logged_in() )
         return $qs;
     $user_ids = array('40','39','37','35');
     $excluded_user=  join(',', $user_ids);
      
     if( $object !='members' )
    	return $qs;
      
     $args=wp_parse_args($qs);
      
     if( !empty( $args[ 'user_id' ] ) )
    	return $qs;
      
     if( !empty( $args['exclude'] ) )
    	$args['exclude'] = $args['exclude'] .','. $excluded_user;
     else
    	$args['exclude'] = $excluded_user;
      
    	$qs = build_query( $args );
      
     return $qs;
      
    }
    add_action('bp_ajax_querystring','ps_exclude_current_user', 21, 2 );

    Prashant Singh
    Participant

    @prashantvatsh

    Hi,

    It is working fine for me. I have just tested on my localhost. Are they coming on second or third page in pagination? You can try clearing cache as well.

    Thanks


    amkh
    Participant

    @amkh

    I am not able to reply to this post with my code..i keep getting ERROR: Your reply cannot be created at this time.


    Prashant Singh
    Participant

    @prashantvatsh

    You can paste that here https://pastebin.com/ and just share the link.


    amkh
    Participant

    @amkh


    amkh
    Participant

    @amkh

    is it because of above code?. hence it is not working?


    amkh
    Participant

    @amkh

    I have this working code to display members directory gender specific. For males a directory of females and vice versa. Maybe this code is the one causing your code not to work?


    Prashant Singh
    Participant

    @prashantvatsh

    add_filter ('bp_ajax_querystring', 'modify_members_loop', 20, 2);
    function modify_members_loop ($qs=false, $object=false)
    {
        global $wpdb;
        if ($object != 'members')  return $qs;
     
        // figure out if the logged-in user is male or female
        $gender = xprofile_get_field_data (9, bp_loggedin_user_id ());
     
        if ($gender == 'Male')
            $query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 9 AND value = 'Female'";
        else
            $query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 9 AND value = 'Male'";
     
        $custom_ids = $wpdb->get_col ($query);
     
        $args = wp_parse_args ($qs);
        $args['include'] = $custom_ids;
        $qs = build_query ($args);
     
        return $qs;
    }

    If it is just one user id then no need to use join code. I have pasted the code after removing that.

    Thanks


    Prashant Singh
    Participant

    @prashantvatsh

    I have this working code to display members directory gender specific. For males a directory of females and vice versa. Maybe this code is the one causing your code not to work?

    Yes, this is possible. You can increase the priority to 9999 or 9 from 21 to check if it is working or not.


    amkh
    Participant

    @amkh

    I tried with both 9 and then 9999. In both cases it still does not work.


    Prashant Singh
    Participant

    @prashantvatsh

    Maybe you can comment on the previous code and then try.


    amkh
    Participant

    @amkh

    I tied to combine the two codes into one. But this also didnt worked. Please see my code below:-

    /* Display Members directory based on gender */
    add_filter ('bp_ajax_querystring', 'modify_members_loop', 20, 2);
    function modify_members_loop ($qs=false, $object=false)
    {
    	global $wpdb;
    	if ($object != 'members')  return $qs;
    
    	// figure out if the logged-in user is male or female
    	$gender = xprofile_get_field_data (9, bp_loggedin_user_id ());
    
    	if ($gender == 'Male')
    		$query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 9 AND value = 'Female'";
    	else
    		$query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 9 AND value = 'Male'";
    
    	$custom_ids = $wpdb->get_col ($query);
    	$user_ids = array('40','39','37','35'); 
        $excluded_user=  join(',', $user_ids);
    	$args = wp_parse_args ($qs);
    	$args['include'] = implode (',', $custom_ids);
        $args['exclude'] = implode (',', $excluded_user);
    	$qs = build_query ($args);
    
    	return $qs;
    }
    

    Prashant Singh
    Participant

    @prashantvatsh

    $args['exclude'] = implode (',', $excluded_user);

    No need to implode here and in place of that line please try:

     if( !empty( $args['exclude'] ) )
    	$args['exclude'] = $args['exclude'] .','. $excluded_user;
     else
    	$args['exclude'] = $excluded_user;

    Thanks


    adrihessels
    Participant

    @adrihessels

    Hello Prashant Singh,

    In which file/directory you need to put the code. In other cases i’ve found /wp-content/plugins/bp-custom.php

    Trying this gives an error.

    Question: why is this option to hide adminstrators of other kind of roles not a default parameter?


    Prashant Singh
    Participant

    @prashantvatsh

    Hi,

    Normally it should work in bp-custom.php but if you are not able to run the code you can use this plugin https://wordpress.org/plugins/bp-custom-functionalities/. Using this plugin you can easily exclude any user role(administrator. subscriber etc.) from members directory.

    Thanks


    CloudedDottedMind
    Participant

    @cloudeddottedmind

    Hi @prashantvatsh,
    I’ve tried your plugin. I wanted to set it to show any user that has the “Community Member” role, but when a user has two roles (admin and member for example), they get excluded.

    How may I make it so if a user a the correct role they get show even if their other role is excluded?

    Thank you

Viewing 17 replies - 1 through 17 (of 17 total)
  • You must be logged in to reply to this topic.
Skip to toolbar