update_post_meta not working in wordpress

I have a bit of code in my functions file that is looping over posts and then adding together facebook and google plus likes for the post and storing the value in the post meta, however it has only saved the value in the post_meta once – it is not updating!

What is my problem here, why is it not updating?

Read More

Here is my code:

$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) : $the_query->the_post();

// Get Facebook Likes From FB Graph API
$data = file_get_contents('http://graph.facebook.com/?id='. get_permalink());
$obj = json_decode($data);
$like_no = intval($obj->{'shares'});

$html =  file_get_contents( "https://plusone.google.com/_/+1/fastbutton?url=".urlencode(get_permalink()));
    $doc = new DOMDocument();   $doc->loadHTML($html);
    $counter=$doc->getElementById('aggregateCount');
    $google_no = $counter->nodeValue;

   $shares_total = $like_no + $google_no;

// Add Facebook Likes to Post Meta
update_post_meta(get_the_ID(), '_mn_fb_likes', $shares_total);

endwhile;
wp_reset_postdata();
}

Related posts

Leave a Reply

1 comment

  1. $query->the_post(); should set up the global $post variable for you, so you should be able to replace

    update_post_meta(get_the_ID(), '_mn_fb_likes', $shares_total);
    

    with

    update_post_meta( $post->ID, '_mn_fb_likes', $shares_total );
    

    If the problem is your get_the_ID() call, this should fix it. (I’m not 100% sure that’s the issue, but at least this will eliminate it as a potential culprit.)