I have created a solution for this and this works. but like count only changes when pages are refresh. any solution for that?
// CODE TO GET LIKES COUNT
## create a table wp_likes
function custom_activity_content_below( $activity_id ) {
// echo bp_activity_id();
global $wpdb;
// Query to count the number of favorites for a specific activity
$favorite_count = $wpdb->get_var( $wpdb->prepare(
”
SELECT COUNT(*)
FROM {$wpdb->prefix}likes
WHERE activity_id = %d
“,
bp_get_activity_id()
) );
echo ‘<div class=”like_count” style=”margin-top: 10px; color: black;”>’.$favorite_count .’ Likes</div>’;
}
add_action( ‘bp_activity_entry_meta’, ‘custom_activity_content_below’,10,1 );
function get_activity_favorite_count( $activity_id ) {
global $wpdb;
// Query to count the number of favorites for a specific activity
$favorite_count = $wpdb->get_var( $wpdb->prepare(
”
SELECT COUNT(*)
FROM {$wpdb->prefix}bp_activity_user_favorite
WHERE activity_id = %d
“,
$activity_id
) );
return $favorite_count;
}
function save_activity_like_to_table($activity_id) {
global $wpdb;
// Get current user ID
$user_id = get_current_user_id();
// Table name
$table_name = $wpdb->prefix . ‘likes’;
// Insert data into the custom table
$wpdb->insert(
$table_name,
array(
‘activity_id’ => $activity_id,
‘user_id’ => $user_id,
‘created_at’ => current_time( ‘mysql’ ),
),
array(
‘%d’,
‘%d’,
‘%s’,
)
);
}
add_action( ‘bp_activity_add_user_favorite’, ‘save_activity_like_to_table’, 10, 1 );
function delete_activity_like_from_table($activity_id) {
global $wpdb;
// Get current user ID
$user_id = get_current_user_id();
// Table name
$table_name = $wpdb->prefix . ‘likes’;
// Delete data from the custom table
$wpdb->delete(
$table_name,
array(
‘activity_id’ => $activity_id,
‘user_id’ => $user_id,
),
array(
‘%d’,
‘%d’,
)
);
}
add_action( ‘bp_activity_remove_user_favorite’, ‘delete_activity_like_from_table’, 10, 1 );