Skip to:
Content
Pages
Categories
Search
Top
Bottom

Using bp_parse_args() to filter members loop


  • johnkim1968
    Participant

    @johnkim1968

    Hi,

    I’m using BP 2.1.1 and I’m trying to use bp_parse_args() to filter all member loops to show only the members with xprofile field id=’3′ and field value=’Woman’.

    So far I have put the following code in BP-Custom but it doesn’t work. Can someone tell me what I am doing wrong? Thanks.

    // Show Only Women
    function see_woman_gender( $retval ) {
         $retval['field_id'] = '3';
         $retval['field_value'] = 'Woman';
     
        return $retval;
    }
    add_filter( 'bp_after_has_members_parse_args', 'see_woman_gender' );
Viewing 13 replies - 1 through 13 (of 13 total)

  • shanebp
    Moderator

    @shanebp

    Please use the code button when sharing code.

    The fields field_id and field_value do not exist in the function you are attempting to filter.
    See this for accepted fields:
    https://buddypress.trac.wordpress.org/browser/tags/2.2.1/src/bp-members/bp-members-template.php#L453

    I think you want this field retval['include'].

    Something like:

    function see_woman_gender( $retval ) {
    	global $wpdb;
    
    	$field_id = 3;
    	$field_value = 'Woman';
    	
    	$query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id . ' AND field_value = ' . $field_value;
    
    	$woman_ids = $wpdb->get_col( $query );
      
    	if ( !empty( $woman_ids ) ) 
    		$retval['include'] = $woman_ids;
    
    	return $retval;
    }
    add_filter( 'bp_after_has_members_parse_args', 'see_woman_gender' );

    There are field re meta data; I don’t think they work with profile data.
    But you could try this and let us know if it works:

    function see_woman_gender( $retval ) {
         $retval['meta_key'] = '3';
         $retval['meta_value'] = 'Woman';
     
        return $retval;
    }
    add_filter( 'bp_after_has_members_parse_args', 'see_woman_gender' );

    johnkim1968
    Participant

    @johnkim1968

    Hi Shane,

    Thanks for the quick reply! I’ll try to use the code function. The second code didn’t work and the first code gave me the error

    Parse error: syntax error, unexpected ‘=’ in /home/mysite/public_html/wp-content/plugins/bp-custom.php on line 26

    Line 26 is

    retval['include'] = $woman_ids;

    Full code below. Any ideas? Appreciated…

    <?php
     
    if ( !defined( 'BP_AVATAR_THUMB_WIDTH' ) )
    define( 'BP_AVATAR_THUMB_WIDTH', 120 ); //change this with your desired thumb width
     
    if ( !defined( 'BP_AVATAR_THUMB_HEIGHT' ) )
    define( 'BP_AVATAR_THUMB_HEIGHT', 120 ); //change this with your desired thumb height
     
    if ( !defined( 'BP_AVATAR_FULL_WIDTH' ) )
    define( 'BP_AVATAR_FULL_WIDTH', 580 ); //change this with your desired full size,weel I changed it to 260 <img src="http://buddydev.com/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley"> 
     
    if ( !defined( 'BP_AVATAR_FULL_HEIGHT' ) )
    define( 'BP_AVATAR_FULL_HEIGHT', 580 ); //change this to default height for full avatar
    
    function see_woman_gender( $retval ) {
    	global $wpdb;
    
    	$field_id = 3;
    	$field_value = 'Woman';
    	
    	$query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id . ' AND field_value = ' . $field_value;
    
    	$woman_ids = $wpdb->get_col( $query );
      
    	if ( !empty( $woman_ids ) ) 
    		retval['include'] = $woman_ids;
    
    	return $retval;
    }
    add_filter( 'bp_after_has_members_parse_args', 'see_woman_gender' );
    
     
    ?>

    shanebp
    Moderator

    @shanebp

    retval['include'] = $woman_ids;
    should be
    $retval['include'] = $woman_ids;


    johnkim1968
    Participant

    @johnkim1968

    Hello Shane,

    That code didn’t work. However, the following code does work. One problem, when filtering (ex. Type=Online, or Type=Newest) it ignores the code and the gender returns. Any idea why? Thanks.

    PS. I put in Pastebin because I got an error when I put it in forum code.

    http://pastebin.com/ASHSkNXF


    shanebp
    Moderator

    @shanebp

    I wrote the class BP_Custom_User_Ids example.

    But for what you want, bp_parse_args is much more appropriate.

    Change the $query to this:

    $query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id . " AND value = '" . $value . "'";


    shanebp
    Moderator

    @shanebp

    And $field_value = 'Woman'; to $value = 'Woman';


    johnkim1968
    Participant

    @johnkim1968

    Hello Shane,

    It works. Thanks. However after extensive testing, I’ve found that it affects the friend request function loop. Basically, all members end up requesting a friend request after applying the code. I’ve attached without_code and with_code screenshot examples. Any ideas how I can fix that? Thanks so much.

    http://tinypic.com/r/312bh4p/9

    http://tinypic.com/r/33m3fnk/9


    shanebp
    Moderator

    @shanebp

    That’s a nasty side effect.
    It’s probably related to some other custom code.
    Does it also do that if you use the default members-loop layout?

    You might try changing the filter call to:
    add_filter( ‘bp_before_has_members_parse_args’, ‘see_woman_gender’ );

    btw – your first linked image is empty.


    johnkim1968
    Participant

    @johnkim1968

    Hello Shane,

    Sorry about that. Here is the link again. There is no visible problem in the members-loop layout, only women show as planned. I isolated the function and the problem persists, so I know it is not other custom code. That filter adjustment didn’t work. I’m going to keep working on it, if another solution comes to mind, please let me know. Thanks!

    before_code


    johnkim1968
    Participant

    @johnkim1968


    shanebp
    Moderator

    @shanebp

    I cannot duplicate your issue using a standard WP theme, like 2015, and no custom code other than the filter discussed in this thread.

    I suspect an issue with your customized version of the members loop or some custom function re members and / or friendships.


    johnkim1968
    Participant

    @johnkim1968

    Hi Shane,

    I tried the 2015 theme and had the same problem. Just to confirm, http://yourURL/members/member name/friends/requests/ – gave you the correct friend requests and not all members? If so, you are correct and I need to keep digging. Thanks.


    johnkim1968
    Participant

    @johnkim1968

    Hello Shane,

    I think you are correct. It’s the members loop or some custom function . I’m unable to find it so far, but I’m determined to move ahead. Question for you – If I want an if-then statement so Men can only see Women and Women can see both Men/Women.

    Something like…

    http://pastebin.com/A20i9zcM

    How can I accomplish this?

    Thanks!

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