List of popular posts counting comments from facebook

Asked

Viewed 161 times

1

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 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 tried to use the answer of this question, and was thus:

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!

EDITION: VAR_DUMP($loopMostCommented)

Object(Wp_query)#3605 (46) { ["query_vars"]=> array(62) { ["posts_per_page"]=> int(5) ["meta_key"]=> string(23) "facebook_comments_count" ["orderby"]=> string(10) "meta_value" ["order"]=> string(4) "DESC" ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["Attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["Static"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["Second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(0) "" ["tag_id"]=> string(0) "" ["Author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["comments_popup"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["s"]=> string(0) "" ["sentence"]=> string(0) "" ["Fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["ignore_sticky_posts"]=> bool(false) ["suppress_filters"]=> bool(false) ["cache_results"]=> bool(false) ["update_post_term_cache"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["post_type"]=> string(0) "" ["nopaging"]=> bool(false) ["comments_per_page"]=> string(2) "50" ["no_found_rows"]=> bool(false) } ["tax_query"]=> Object(Wp_tax_query)#3572 (6) { ["queries"]=> array(0) { } ["relation"]=> string(3) "AND" ["table_aliases:protected"]=> array(0) { } ["queried_terms"]=> array(0) { } ["primary_table"]=> string(8) "wp_posts" ["primary_id_column"]=> string(2) "ID" } ["meta_query"]=> Object(Wp_meta_query)#3573 (7) { ["queries"]=> array(2) { [0]=> array(1) { ["key"]=> string(23) "facebook_comments_count" } ["relation"]=> string(2) "OR" } ["relation"]=> string(3) "AND" ["meta_table"]=> string(11) "wp_postmeta" ["meta_id_column"]=> string(7) "post_id" ["primary_table"]=> string(8) "wp_posts" ["primary_id_column"]=> string(2) "ID" ["table_aliases:protected"]=> array(1) { [0]=> string(11) "wp_postmeta" } ["date_query"]=> bool(false) ["post_count"]=> int(0) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["found_posts"]=> int(0) ["max_num_pages"]=> int(0) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(false) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(false) ["is_tag"]=> bool(false) ["is_tax"]=> bool(false) ["is_search"]=> bool(false) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(true) ["is_404"]=> bool(false) ["is_comments_popup"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash:private"]=> string(32) "a186c4864cb9d6fa90fd5b23b6fc8fa2" ["query_vars_changed:private"]=> bool(false) ["thumbnails_cached"]=> bool(false) ["stopwords:private"]=> NULL ["query"]=> array(4) { ["posts_per_page"]=> string(1) "5" ["meta_key"]=> string(23) "facebook_comments_count" ["orderby"]=> string(10) "meta_value" ["order"]=> string(4) "DESC" } ["request"]=> string(365) "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'Publish' OR wp_posts.post_status = 'private') AND ( wp_postmeta.meta_key = 'facebook_comments_count' ) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value DESC LIMIT 0, 5" ["posts"]=> array(0) { } }

Note: I don’t be reading this yet =/

  • if you var_dump $loopMostCommented what is the result?

  • @Ricardobrgweb I did what you suggested and the result I posted above. I hope you can help me and I’m sorry for the delay.

  • Your wp_query did not return any post. Probably the error is in creating the facebook_comments_count field because you’re using FQL that doesn’t work for new apps. You will need to use the Graph API https://developers.facebook.com/docs/graph-api/reference/v2.2

  • I don’t know how to use it, can you help me? @Ricardobrgweb

  • Hi Luna, I have no time to study the Graph api now, but as soon as I get it I put the answer here...

No answers

Browser other questions tagged

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