0

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.

5
  • What is the graph request returning? Apr 13, 2015 at 20:06
  • You might want to save a timestamp and only check so often - if you keep hammering the API on every request you'll soon end up getting blocked/temporarily suspended (depending on the traffic to your site). Apr 13, 2015 at 20:36
  • The graph request returns an integer of the number of comments.
    – eliwedel
    Apr 14, 2015 at 1:53
  • How do I include a timestamp in this code? Again, I know some php but I don't really know the wordpress backend.
    – eliwedel
    Apr 14, 2015 at 1:54
  • @eliwedel You can edit this plug-in [Top Shared Posts on Facebook][1] to retrieve data for comment_count rather than total_count which uses by default. [1]: wordpress.org/plugins/top-shared-posts-on-facebook
    – Ismail
    May 4, 2015 at 2:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.