Skip to:
Content
Pages
Categories
Search
Top
Bottom

Filtering Members Loop with Custom Data and URL Querystring


  • AntonyC
    Participant

    @antonyc

    Hi,

    I’m running BuddyBoss/BuddyPress on WordPress 5.3.2

    I’d like to be able to filter my main BuddyPress members page using $_GET, from query variables passed in the URL.

    I’ve tried various ideas found on the web and the most up to date/promising way of doing this seems to be using bp_parse_args() via…

    add_filter( 'bp_after_has_members_parse_args', 'my_function' );

    Using this method works well except for the fact that I am unable to access my passed filter variables via $_GET …if I print the site url from within my function it returns wp-admin/ajax.php (or something like it) which I guess is why I can’t access the passed query variables. Any help with this would be greatly appreciated.

    Many thanks,

    Antony

    My code…

    function ic_members_filter( $retval ) {
    
        global $wpdb;
        
        global $ic_filter_members_interest_id;
    
        if( $ic_filter_members_interest_id &&  $ic_filter_members_interest_id>0 ){
     
            $prepared_statement = $wpdb->prepare( "SELECT user_id FROM ic_interests_data WHERE interest_id = %d AND (interest_value = 1 OR interest_value = 3)", $ic_filter_members_interest_id );
            $db_custom_ids = $wpdb->get_col( $prepared_statement );
            
    
            if ( $db_custom_ids ) { //!empty( $db_custom_ids )
                
                // convert the array to a csv string
                $retval['include'] =  implode(",", $db_custom_ids);  //$custom_ids_str = 'include=' . implode(",", $custom_ids);
            }
            else{
                
                // don't show any records
                $retval['include'] = array(0);
            }
    
        }else{
            // show all records
            $retval['include'] = '';
        }
     
        return $retval;
    }
    add_filter( 'bp_after_has_members_parse_args', 'ic_members_filter' );

    WHERE… $ic_filter_members_interest_id is a global that holds the interest_id via $_GET

    References…

    Using bp_parse_args() to filter BuddyPress template loops


    https://buddypress.trac.wordpress.org/browser/tags/2.2.1/src/bp-members/bp-members-template.php#L461

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

  • shanebp
    Moderator

    @shanebp

    It sounds like the issue is with your variable: $ic_filter_members_interest_id;

    Have you confirmed that it is being set? If it is not, then this is not a BP issue.

    function ic_members_filter( $retval ) {
        global $ic_filter_members_interest_id;
    
        echo '<br>ic_filter_members_interest_id: ' . $ic_filter_members_interest_id;
    
        return $retval;
    }
    add_filter( 'bp_after_has_members_parse_args', 'ic_members_filter' );

    AntonyC
    Participant

    @antonyc

    Hi @shanebp, thanks for your quick response.

    Yes, I can but I’ve found that because wordpress loads the theme functions.php/plugins 2-3 times the variable will set ok on one of these iterations but not on the iteration where bp_after_has_members_parse_args() is fired (not sure how best to explain this as not a pro wp developer but have read about this phenomena online (I have disabled plugins/theme to check it’s nothing to do with my code causing multiple loads). I’m assuming that functions.php is loaded again when the members ajax call is made?

    So how to capture using $_GET and then make this available to bp_after_has_members_parse_args()?

    Thanks again,
    Antony


    shanebp
    Moderator

    @shanebp

    Try putting the code that sets $ic_filter_members_interest_id in bp-custom.php or in a separate plugin.

    And put it in a function that uses a BP hook.

    function set_ic_members_var() {
        //set your global var here
    }
    add_action( 'bp_ready', 'set_ic_members_var' );

    AntonyC
    Participant

    @antonyc

    Thanks for this suggestion, unfortunately I have the same issue …I placed both functions in bp-custom.php and this is my debug log (you can see the multiple loads of these functions)…

    [23-Feb-2020 18:34:27 UTC] >>>>>>> ic-members-main.php::  $ic_filter_members_interest_id:9  >>>> 02/23/2020 06:34:27 pm <<<<
    [23-Feb-2020 18:34:28 UTC] >>>>>>> set_ic_members_var::  $ic_filter_members_interest_id:9  >>>> 02/23/2020 06:34:28 pm <<<<
    [23-Feb-2020 18:34:33 UTC] >>>>>>> ic-members-main.php::  $ic_filter_members_interest_id:  >>>> 02/23/2020 06:34:33 pm <<<<
    [23-Feb-2020 18:34:33 UTC] >>>>>>> ic_members_filter::  $ic_filter_members_interest_id:  >>>> 02/23/2020 06:34:33 pm <<<<
    [23-Feb-2020 18:34:33 UTC] >>>>>>> ic_members_filter::  $ic_filter_members_interest_id:  >>>> 02/23/2020 06:34:33 pm <<<<
    [23-Feb-2020 18:34:33 UTC] >>>>>>> ic_members_filter::  $ic_filter_members_interest_id:  >>>> 02/23/2020 06:34:33 pm <<<<

    AntonyC
    Participant

    @antonyc

    …apologies, I noticed had another function still running – still not working but here is a cleaner log with just the two functions…

    [23-Feb-2020 18:42:39 UTC] >>>>>>> set_ic_members_var::  $ic_filter_members_interest_id:9  >>>> 02/23/2020 06:42:39 pm <<<<
    [23-Feb-2020 18:42:44 UTC] >>>>>>> ic_members_filter::  $ic_filter_members_interest_id:  >>>> 02/23/2020 06:42:44 pm <<<<
    [23-Feb-2020 18:42:44 UTC] >>>>>>> ic_members_filter::  $ic_filter_members_interest_id:  >>>> 02/23/2020 06:42:44 pm <<<<
    [23-Feb-2020 18:42:44 UTC] >>>>>>> ic_members_filter::  $ic_filter_members_interest_id:  >>>> 02/23/2020 06:42:44 pm <<<<

    AntonyC
    Participant

    @antonyc

    …my full code currently in bp-custom.php…

    function set_ic_members_var() {
        //set your global var here
        
        global $ic_filter_members_interest_id;
        
        if (isset($_GET["ia"])) {
            $ic_filter_members_interest_id = $_GET["ia"];
        } else {
            $ic_filter_members_interest_id = 'unset!';
        }
    error_log('>>>>>>> set_ic_members_var::  $ic_filter_members_interest_id:'.$ic_filter_members_interest_id.'  >>>> '.$date = date('m/d/Y h:i:s a', time()).' <<<<');
        
    
    }
    add_action( 'bp_ready', 'set_ic_members_var' );
    
    function ic_members_filter( $retval ) {
    
        global $wpdb;
        
        global $ic_filter_members_interest_id;
    
    error_log('>>>>>>> ic_members_filter::  $ic_filter_members_interest_id:'.$ic_filter_members_interest_id.'  >>>> '.$date = date('m/d/Y h:i:s a', time()).' <<<<');
    
        if( $ic_filter_members_interest_id &&  $ic_filter_members_interest_id>0 ){
     
            $prepared_statement = $wpdb->prepare( "SELECT user_id FROM ic_interests_data WHERE interest_id = %d AND (interest_value = 1 OR interest_value = 3)", $ic_filter_members_interest_id );
            $db_custom_ids = $wpdb->get_col( $prepared_statement );
            
    
            if ( $db_custom_ids ) { //!empty( $db_custom_ids )
                
                // convert the array to a csv string
                $retval['include'] =  implode(",", $db_custom_ids);  //$custom_ids_str = 'include=' . implode(",", $custom_ids);
            }
            else{
                
                // don't show any records
                $retval['include'] = array(0);
            }
    
        }else{
            // show all records
            $retval['include'] = '';
        }
     
        return $retval;
    }
    add_filter( 'bp_after_has_members_parse_args', 'ic_members_filter' );

    AntonyC
    Participant

    @antonyc

    UPDATE:: I tested this code on a BuddyPress installation I have and get the same results so not a buddyboss issue.


    AntonyC
    Participant

    @antonyc

    Hi @shanebp, wondering if you have any more thoughts on how I may proceed with this? Cheers, Ant

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