BuddyDev

Search

Badged users coming to the top of searches. – BuddyPress User Badges

  • Participant
    Level: Initiated
    Posts: 5
    Mark Lawrence on #30298

    Is it possible with the badges plugin that in searches the badged user will come up first e.g. search for someone that prints business cards, the member with a badge will come up first?

    Thanks for your help

    Mark

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #30326

    Hi Mark,
    Thank you for the question.
    It might be doable. I will need couple of days before I can check it and post some code.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    Mark Lawrence on #30337

    THanks so much thats amazing. Cheers for this, please let me know if I can help in any way. Have a great weekend. 🙂

  • Participant
    Level: Initiated
    Posts: 5
    Mark Lawrence on #30436

    Hi Brajesh

    Just seeing if you had any luck with this feature.

    Thanks for looking into this.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2934
    Ravi on #30470

    Hello Mark,

    Please try the following code in your bp-custom.php file. It will list members with most badges on top.

    
    add_filter( 'bp_has_members', function ( $has_members, $members_template ) {
    
    	if ( ! $has_members || ! function_exists( 'bp_user_badges_get_user_badges' ) ) {
    		return $has_members;
    	}
    
    	if ( ! bp_is_members_directory() ) {
    		return $has_members;
    	}
    
    	usort( $members_template->members, function ( $first, $second ) use ( $members_template ) {
    
    		if ( count( bp_user_badges_get_user_badges( $second->ID ) ) >= count( bp_user_badges_get_user_badges( $first->ID ) ) ) {
    			return 1;
    		}
    
    		return -1;
    	} );
    
    	return $has_members;
    }, 10, 2 );
    
    

    Let me know if it works or not.

    Regards
    RAvi

  • Participant
    Level: Initiated
    Posts: 5
    Mark Lawrence on #30475

    Hi This is throwing

    There has been a critical error on your website. Please check your site admin email inbox for instructions.

    This is the layout of my bp-custom.php file in plugins

    <?php
    add_filter( ‘bp_docs_get_default_access_options’, ‘my_change_docs_default_access_levels’ );
    function my_change_docs_default_access_levels( $defaults ) {
    $defaults[‘read’] = ‘loggedin’;
    $defaults[‘edit’] = ‘creator’;
    $defaults[‘read_comments’] = ‘loggedin’;
    $defaults[‘view_history’] = ‘creator’;
    return $defaults;
    }

    /*allowing badges to be shown first in the searches*/
    add_filter( 'bp_has_members', function ( $has_members, $members_template ) {

    if ( ! $has_members || ! function_exists( 'bp_user_badges_get_user_badges' ) ) {
    return $has_members;
    }

    if ( ! bp_is_members_directory() ) {
    return $has_members;
    }

    usort( $members_template->members, function ( $first, $second ) use ( $members_template ) {

    if ( count( bp_user_badges_get_user_badges( $second->ID ) ) >= count( bp_user_badges_get_user_badges( $first->ID ) ) ) {
    return 1;
    }

    return -1;
    } );

    return $has_members;
    }, 10, 2 );
    ?>

    I have another function at the top. Im also using the plugin bp profile search. Which I think uses filters, could this be the problem?

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2934
    Ravi on #30476

    Hello Mark,

    Sorry for the inconvenience. I have tested this with Profile search and it is working fine. Please check error logs what errors it is throwing.

    Also, remove ‘?>’ from the bottom of your ‘bp-custom.php’ file.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 5
    Mark Lawrence on #30477

    No problem at all thanks so much for your help. This is a really great feature.

    I still get the same error after removing the ‘?>’

    How can I check the error logs. Little new to this bit.

    Regards

    Mark

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #30493

    Hi Mark,
    Please share the complete code from your bp-custom.php(all content from the file including tags).
    You can put it on pastebin.com and link me. That will help us assist you quickly.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved