I have modified a widget to display the total number of Facebook comments on a post rather than the total wordpress comments. I can get that part to show correctly, but I would like to get the most popular posts based on how many Facebook comments there are as I have disabled wordpress comments and am only using the Facebook comments plugin v2.3.
This is the code I have searched and modified from various web posts but I know I'm missing something. I have also added a field in the database by duplicating the comment_count field and renaming it facebook_comments_count.
Currently I have this code in functions.php, but I doubt I'm putting it in the correct location.
add_action('template_redirect', 'updateFBCommentsCount', 1, 1);
function updateFBCommentsCount($post_id) {
$url = get_permalink($post_id);
$query = "https://graph.facebook.com/".$url."?fields=comments";
$responseText = file_get_contents($query);
$responseJson = json_decode($responseText);
$commentCount = $responseJson->comments;
update_post_meta($post_id, 'facebook_comments_count', $commentCount);
}
If I could get some direction with this that would be great. I'm not new with php, but new with the backend of wordpress.
comment_count
rather thantotal_count
which uses by default. [1]: wordpress.org/plugins/top-shared-posts-on-facebook