Add filter to only show results of users who were recently active?

BP Profile Search Add filter to only show results of users who were recently active?

  • Author
    Post
  • #12577 Reply
    Lizzie
    Guest

    Hi Andrea,

    Please could you assist me?

    My site has many thousands of profiles, but the vast majority were not recently active. For example, at least 50% were not active during the last 12 months.

    I would like to add a simple checkbox on the search form like “Only show users who were active in the last 12 months” (default should be unchecked)

    This would allow searchers to avoid seeing all those old “abandoned” profiles (they tell me it’s annoying)

    I know it probably involves using function bp_get_user_last_activity($userID) but my php skills are not enough to create this functionality.

    Thankyou!

    #12578 Reply
    andrea
    Plugin Author

    Hi Lizzie,

    Sorry for the late reply!

    I’ve done some research on your question, but I haven’t found a simple way to remove recently inactive users from search results.

    As an alternative, you could advise your users to sort their search results by “Last Active”, so that a phrase like “Active 3 weeks ago” appears under each user’s picture. This way, they can stop looking at subsequent results as soon as they see a phrase like “Active 12 months ago”.

    Hope this helps!

    #12579 Reply
    Lizzie
    Guest

    Hi Andrea,

    That’s a shame! I thought it would be easy to achieve that but I take your word for it.

    I have tried a few attempts with AI to create the code but it didn’t work. Is it worth it for me to post the AI code suggestions here for your comments, or no point really?

    Thanks!

    #12580 Reply
    andrea
    Plugin Author

    Hi Lizzie,

    Yes, you can post the code the AI ​​suggests, maybe it can point me in the right direction!

    #12581 Reply
    Lizzie
    Guest

    Ok here’s an example …

    My prompt …

    Help with WordPress / Buddypress coding please.
    I am using the BP Profile Search plugin.
    I want to add a checkbox to the search form.
    When checked, the search will return only profiles who have been active within the last 90 days.
    The checkbox is unchecked by default.
    The checkbox label should be “Show only recently active profiles”

    AI replies …

    To add a checkbox to the BP Profile Search form that filters profiles based on recent activity, you’ll need to use a custom code snippet. Here’s a step-by-step guide to help you achieve this:
    Step 1: Add the checkbox to the search form
    You can use the bps_add_fields action hook to add a custom field to the search form. Add the following code to your theme’s functions.php file:

    
    function add_recently_active_checkbox( $fields ) {
        $fields['recently_active'] = array(
            'label' => 'Show only recently active profiles',
            'type' => 'checkbox',
            'default' => 0,
        );
        return $fields;
    }
    add_filter( 'bps_add_fields', 'add_recently_active_checkbox' );
    

    Step 2: Modify the search query
    To filter the search results based on the checkbox value, you’ll need to modify the search query using the bps_ajax_query filter. Add the following code to your theme’s functions.php file:

    
    function filter_recently_active_profiles( $args ) {
        if ( isset( $_POST['recently_active'] ) && $_POST['recently_active'] == 1 ) {
            $args['date_query'] = array(
                array(
                    'column' => 'last_activity',
                    'after' => '90 days ago',
                ),
            );
        }
        return $args;
    }
    add_filter( 'bps_ajax_query', 'filter_recently_active_profiles' );
    

    Step 3: Ensure the checkbox state is passed in the search request
    Make sure the checkbox value is sent with the search request. The plugin should handle this automatically, but you can verify by checking the request payload in the browser’s developer tools.
    That’s it! With these code snippets, you should now have a checkbox on your search form that filters profiles based on recent activity.

    #12582 Reply
    Lizzie
    Guest

    My comment : this AI code does not even create a checkbox for me, but I suppose the query code is the important part. Any ideas?

    #12584 Reply
    andrea
    Plugin Author

    Hi Lizzie,

    I’m impressed by the code suggested by the AI, but unfortunately, as you’ve noticed, it’s incorrect.

    However, I have found a solution that should meet your needs:

    add_filter ('bps_add_fields', 'bps_active_setup', 99);
    function bps_active_setup ($fields)
    {
    	$f = new stdClass;
    	$f->group = __('Usermeta data', 'bp-profile-search');
    	$f->code = 'active';
    	$f->name = __('Last Active window (days)', 'bp-profile-search');
    	$f->description = __('User was last active during the specified time period', 'bp-profile-search');
    
    	$f->format = 'integer';
    	$f->options = array ();
    	$f->search = 'bps_active_search';
    
    	$fields[] = $f;
    	return $fields;
    }
    
    function bps_active_search ($f)
    {
    	global $bp, $wpdb;
    
    	$value = $f->value;
    	$filter = $f->format. '_'.  ($f->filter == ''? 'is': $f->filter);
    
    	$sql = array ('select' => '', 'where' => array ());
    	$sql['select'] = "SELECT DISTINCT user_id FROM {$bp->activity->table_name}";
    	$sql['where']['type'] = $wpdb->prepare ("type = 'last_activity'");
    
    	switch ($filter)
    	{
    	case 'integer_is':
    		$sql['where']['active'] = $wpdb->prepare ("DATEDIFF(CURDATE(), date_recorded) <= %d", $value);
    		break;
    
    	case 'integer_range':
    		if (isset ($value['min']))  $sql['where']['active_min'] = $wpdb->prepare ("DATEDIFF(CURDATE(), date_recorded) >= %d", $value['min']);
    		if (isset ($value['max']))  $sql['where']['active_max'] = $wpdb->prepare ("DATEDIFF(CURDATE(), date_recorded) <= %d", $value['max']);
    		break;
    
    	default:
    		return array ();
    	}
    
    	$sql = apply_filters ('bps_field_sql', $sql, $f);
    	$query = $sql['select']. ' WHERE '. implode (' AND ', $sql['where']);
    
    	$results = $wpdb->get_col ($query);
    	return $results;
    }
    

    It adds a new form field to the Usermeta data group, and you can add it to your search form in the usual way.

    It’s not a checkbox, but a text box where you can specify the number of days after which users will no longer be displayed. Please let me know if it works for you!

    #12586 Reply
    Lizzie
    Guest

    Andrea thankyou, it works great, I really appreciate you solving this for me!

    #12587 Reply
    andrea
    Plugin Author

    You’re welcome Lizzie, I’m glad I was able to help!

Reply to: Add filter to only show results of users who were recently active?
My Information