Forum Replies Created

Viewing 1 replies (of 1 total)
  • Neither did I. I had to make some changes to make it work. The post_id didnt update the like count in the facebook_like count cell.

    the page where you make the query, ex page.php

    <ul>
    <li>
    <?php query_posts('meta_key=facebook_likes&orderby=meta_value&posts_per_page=5&cat=6'); ?>
    <?php if( have_posts() ) : while( have_posts() ) : the_post(); ?>
    <?php $permalink = get_permalink( $id ); ?>
    <?php update_facebook_likes("",$permalink, $id); ?>
    <!-- call the update_facebook_likes if you're not using the_content -->
    <!-- include a link to the post and a facebook like button -->
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><div class="fb-like right" data-href='<?php the_permalink(); ?>' data-send="false" data-layout="button_count" data-width="450" data-show-faces="false" data-font="arial"></div>
    <?php endwhile; endif; wp_reset_query(); ?>
    </li>
    </ul>

    What this does is that it calls a function called update_facebook_likes() with 3 parameters.

    Next I took the code from facebook-like-posts-order.php and pasted it in my functions.php file.

    The functions.php file:

    <?php
    function insert_facebook_likes_custom_field($post_ID) {
       global $wpdb;
       if(!wp_is_post_revision($post_ID)) {
          add_post_meta($post_ID, 'facebook_likes', '0', true);
       }
    }
    
    add_action('publish_post', 'insert_facebook_likes_custom_field');
    
    function update_facebook_likes($content = '', $perma, $theid) {
    	$permalink = $perma;
    	$idposts = $theid;
    	$url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($permalink);
    	$xml = file_get_contents($url);
    	$xml = simplexml_load_string($xml);
    
    	$like_no = $xml->link_stat->total_count;
    	//echo "like count = ". $like_no . "<br />";
    	//echo "the id = " . $idposts . "<br />";
    	$meta_values = get_post_meta($idpost, 'facebook_likes', true);
    
    	if($like_no == null){
    		$like_no = 0;
    	}
    
    	update_post_meta( (int) $idposts, 'facebook_likes', (string) $like_no );
    	return $content;
    
    }
    
    add_action('the_content', 'update_facebook_likes');
    ?>

    The update_facebook_likes receives 2 important parameters. The $perma and $theid. The $url uses the $perma to get a callback from facebook. The total_count(likes+shares). If the return values is null(in this case 0), the like count is 0. Otherwise update the post with the new like count so we can order the post by like count. This function updates the like count of the post
    update_post_meta( (int) $idposts, 'facebook_likes', (string) $like_no );

    Hope this will help for someones that stuck.

    cheers
    henrik

Viewing 1 replies (of 1 total)