List of popular posts with facebook comments

2

I'm trying to make a 5-item list of the most commented posts from my blog, but with facebook comments.

I found this code on google, but as you can see, it only captures the comments made on the WP platform itself and not from facebook.

<?php
  $result = $wpdb
       ->get_results("SELECT comment_count, ID, post_title FROM $wpdb
       ->posts ORDER BY comment_count DESC LIMIT 0 , 10");
  foreach ($result as $topten) {
  $postid = $topten->ID;
  $title = $topten->post_title;
  $commentcount = $topten->comment_count;
  if ($commentcount != 0) {
  ?>
<li>
  <a href="<?php echo get_permalink($postid); ?>">< ?php echo $title ?></a>
</li>
<?php } } ?>

I replace all the contents of the "comments.php" file of my WP (twenty fifteen theme) by this one:

<div id="comments" class="comments-area">
    <div id="fb-root"></div>
    <script>
    (function(d, s, id) {
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) return;
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/pt_BR/sdk.js#xfbml=1&appId=779264268806634&version=v2.0";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
    </script>
    <div class="fb-comments" data-href="<?php echo get_permalink(); ?>" data-numposts="10" data-colorscheme="light"></div>
</div>

and to show the number of comments of each post I use this:

<fb:comments-count href=<?php echo get_permalink(); ?>></fb:comments-count>

I would like to know how to do the listing, I already searched google but I did not find anything.

Thank you in advance.

MODIFICATION

According to @Lollipop's answer and some google searches, I made the following changes:

I added this function in functions.php :

if (!function_exists('fb_comment_count')) {
     function fb_comment_count() {
        global $post;
        $url = get_permalink($post->ID);

        $query = "SELECT comment_count FROM link_stat WHERE url = '{$url}'";
        $responseText = file_get_contents('https://graph.facebook.com/fql?q='.$query);
        $responseJson = json_decode($responseText);

        $commentCount = $responseJson->data->comment_count;
        update_post_meta($post->ID, 'facebook_comments_count', $commentCount);

        return;
    }
}

and in the file where I want to appear the listing I put:

  <ul>
    <?php
      $loopMostCommented = new WP_Query('posts_per_page=5&meta_key=facebook_comments_count&orderby=meta_value&order=DESC');
        while ($loopMostCommented->have_posts()) : $loopMostCommented->the_post();
    ?>
    <li>
      <a class="transition-2s" title="<?php the_title(); ?>" href="<?php url() ?>" rel="bookmark"> 
        <span class="reclink"><?php the_title(); ?></span>
      </a>
    </li>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
  </ul>

Unfortunately, nothing appears within the tag " < li > ". Can anyone help me?

Thanks in advance!

    
asked by anonymous 12.02.2015 / 23:20

1 answer

1

Save the number of comments in post meta-data so that you are able to use it for sorting purposes later.

We should only retrieve the comment count:

SELECT comment_count FROM link_stat WHERE url = 'POST_URL'
function fb_comment_count() {
  global $post;
  $url = get_permalink($post->ID);

  $query = "SELECT comment_count FROM link_stat WHERE url = '{$url}'";
  $responseText = file_get_contents('https://graph.facebook.com/fql?q='.$query);
  $responseJson = json_decode($responseText);

  $commenteCount = $responseJson->data->commentsbox_count;
  update_post_meta($post->ID, 'facebook_comments_count, $commenteCount);
  // ...
}

Once you have a count, now loop:

query_posts('posts_per_page=5&meta_key=facebook_comments_count&orderby=meta_value&order=DESC')

source:

link

    
19.02.2015 / 17:16