Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i want to make custom filed that contains facebook like count for the post url to get the likes i used this code

function fb_count(){
$link = get_permalink($post->ID);
$content = file_get_contents("http://api.facebook.com/restserver.php?method=links.getStats&urls=".$link);
$element = new SimpleXmlElement($content);
$shareCount = $element->link_stat->total_count;
return $shareCount;}

which i don't know how to store the Facebook data in custom filed i tried this code but it dos not work

<?php $like_key = 'likes_count';
     $link = get_permalink($id);
$content = file_get_contents("http://api.facebook.com/restserver.php?   method=links.getStats&urls=".$link);
$element = new SimpleXmlElement($content);
$shareCount = $element->link_stat->total_count;
 $key1_values = get_post_custom_values($like_key, $id);
    foreach ( $key1_values as $value )
     update_post_meta($id,$like_key,$shareCount, $value); ?>

i wanna know i what i am doing wrong

share|improve this question

3 Answers 3

I've done it, here is the complete code:

function insert_facebook_likes_custom_field($post_ID) {
    global $wpdb;
    if (!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'likes_count', '0', true);
    }
}
add_action('publish_page', 'insert_facebook_likes_custom_field');
add_action('publish_post', 'insert_facebook_likes_custom_field');

function update_facebook_likes($content = '') {
    global $wp_query;
    $permalink = get_permalink();
    $idpost = $wp_query->post->ID;
    $data = file_get_contents('http://graph.facebook.com/?id='.$permalink);
    $json = $data;
    $obj = json_decode($json);
    $like_no = $obj->{'shares'};
    $meta_values = get_post_meta($idpost, 'likes_count', true);
    if ($like_no == null) {
        $like_no = 0;
    }
    update_post_meta($idpost, 'likes_count', $like_no, false);
    return $content;
}
add_action('the_content', 'update_facebook_likes');

Just copy/paste this code into functions.php. The data will store in a custom field called likes_count. Hope it helps

share|improve this answer

For display number of Facebook fans in full text, i used this part of code :

$page_id = "YOUR PAGE-ID";
$xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$page_id."") or die ("a lot");
$fans = $xml->page->fan_count;
echo $fans;

"YOUR PAGE-ID" -> the ID of Facebook page. I hope to be useful.

share|improve this answer
    
You could also cache the Number, so you improve the performance because not connecting to facebook every time. –  fischi Dec 27 '12 at 14:42

See Getting Facebook Likes, Shares and Comment Counts Using PHP Function.
Its a newer version where you can get the facebook likes, comment, and share counts using facebook FQL and get it via json and display it as a text. you can manipulate the view and design it in a way you want. Its so handy, simpler Code, and soo useful

share|improve this answer
    
Please post an example of the code one might use –  bungeshea_ Jan 13 '13 at 4:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.