Get ‘Favorite’ usernames of Activity | Buddypress Custom functions By Rimon Habib

In Buddypress activity steam, Buddypress provides you a Favorite button which is similar to Facebook ‘Like’. When any user click on Favorite button, an activity filter tab opened named ‘favorites’ which contain all favorite activities of that user.

Now Whats about Activity’s perspective? I mean, how can I show user names just below these activity meta button (Comment, Favorite etc)? there no default way, but lets write a function or may be plugin for it. First we need to get users who Favorited a particular activity.

When user’s favorite activities information are saved as an array in wp_usermeta table, which meta key is ‘bp_favorite_activities’
So we can get user ID from this table if I know activity ID, we can get activity ID simply calling bp_get_activity_id() inside the loop, and as bp_favorite_activities saved as an array in database, we can user LIKE command with activity id to know if that particular activity id exist on that array or not..yes, we could also do that with php array matching function, but it is faster I think.

so.. the query will be,


[codesyntax lang=”php”]
SELECT user_id FROM wp_usermeta WHERE meta_key = ‘bp_favorite_activities’ AND meta_value LIKE ‘%:”$activity_id”;%

Now, we got user Id’s using that query. we want to show user’s display name after activity who liked that before. so we need
user’s display name; we can use this BP function to get user’s display name

[codesyntax lang=”php”]


‘field’ => 1,
‘user_id’ => $user_id




we also want to link up this names to user’s profile page too, so, we need user’s profile page’s link. we can use this..

[codesyntax lang=”php”]

$link = bp_core_get_user_domain($user_id);


The whole function can be written as:



[codesyntax lang=”php”]

function get_users_fav($activity_id='')
$activity_id = bp_get_activity_id();
global $wpdb;
$query = "SELECT user_id FROM ".$wpdb->base_prefix."usermeta WHERE meta_key = 'bp_favorite_activities' AND meta_value LIKE '%:"$activity_id";%' ";
$users = $wpdb->get_results($query,ARRAY_A);
foreach ($users as $user)
$name = bp_get_profile_field_data(
'field' => 1,
'user_id' => $user['user_id']
$link = bp_core_get_user_domain($user['user_id']);
$u_names[++$i] = '<a class="activity_fav_users" href="'.$link.'"'.">$name</a>";
echo '<div class="fav_box">Favorite of: '.implode(',', $u_names).'</div>';
return '';



Now, need to hook up with BP filter so that the result will be shown at the end of each activity.

add_filter( ‘bp_activity_entry_meta’, ‘get_users_fav’,99 );

That’s all.. you can copy this function to your theme’s function.php file, and you have nothing more to do. 🙂

we can transform it as a plugin, so it will be more flexible. you can download it from here

3 comments on “Get ‘Favorite’ usernames of Activity | Buddypress Custom functions By Rimon Habib

  1. Great stuff! But is there also a way to get the total number of favorites so that it displays next to the favorite button just like the comment button has?

  2. Hi Rimon!

    Nice hack! 🙂
    Only issue: Some of the names disappear!
    It seems to me that it has to do with someone liking several items..
    My count still works correctly, but the names arent’t shown any longer.

    Do you have any idea on how to solve this?

    Thanks in advance!

  3. hello, please am using buddypress 1.8, i want to know how to get a user’s total accumulated favorites from all his activities e.g:
    all activities=30
    all the favorites from all activities= 300.

    i would also like to know how to get a user’s total activities. eg:
    user1=20 activities.
    user2=40 activities.