List of popular posts with facebook comments

Asked

Viewed 439 times

2

I’m trying to make a list of 5 items of the most commented posts on my blog, but with the comments on facebook.

I found this code on google, but as you can see, it just captures the comments made on the WP platform itself and not on 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 replaced all the contents of the file "comments.php" 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’ve searched on google but found nothing.

Thank you in advance.

MODIFICATION

According to @Lollipop’s reply and some google searches, I made the following modifications:

I added this function to 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 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 inside the tag "< li >". Can someone help me?

Thank you so much!

1 answer

1

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

We should just get the commentary count back:

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 make a loop:

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

source:

https://stackoverflow.com/questions/8919791/wordpress-show-popular-posts-based-on-facebook-comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.