// Shortcode: [author_total_likes author_id="123"]
add_shortcode('author_total_likes', function($atts) {
global $wpdb;
$atts = shortcode_atts([
'author_id' => 0,
], $atts);
$author_id = intval($atts['author_id']);
if (!$author_id) return '0';
// Table name
$table_name = $wpdb->prefix . 'post_like_counts';
// Get all post IDs by this author
$post_ids = $wpdb->get_col($wpdb->prepare("
SELECT ID FROM {$wpdb->posts}
WHERE post_author = %d
AND post_status = 'publish'
AND post_type = 'post'
", $author_id));
if (empty($post_ids)) return '0';
// Sum like counts from custom table
$total_likes = $wpdb->get_var("
SELECT SUM(like_count)
FROM $table_name
WHERE post_id IN (" . implode(',', array_map('intval', $post_ids)) . ")
");
// Format result
$total_likes = $total_likes ? intval($total_likes) : 0;
// Optional: display formatted numbers (e.g., 1.2K, 3.4M)
if ($total_likes >= 1000000) {
$total_likes = number_format($total_likes / 1000000, 1) . 'M';
} elseif ($total_likes >= 1000) {
$total_likes = number_format($total_likes / 1000, 1) . 'K';
}
return $total_likes;
});