0

I am trying to store SNS share value + comment count on my postmeta, DB. I put code below to accomplish this task,

function get_sns_share($post_ID=0) { 
    $url1=get_permalink($post->ID);
    $src = json_decode(file_get_contents('http://graph.facebook.com/' . $url1));
    $share_count = $src->shares; // facebook commnet + comment like + share + like
    $my_var = get_comments_number( $post_id ); // comment count
    $social_score = $share_count + $my_var + $tweet; // social score sum
        if ($social_score > 999) $social_socre = 999;

    return $social_score;

update_post_meta($post_id, '_social_score', $social_score);
}

The problem is this code certainly do works every field other than update_post_meta. It seems it does not add add post meta field to my database, thus I cannot get any value from there. Your help would be much appreciated.

1

1 Answer 1

1

Within a function or method, the return statement immediately stops the execution of the earlier.
Hence the last line never gets interpreted.

Switch the order of this:

return $social_score; 
update_post_meta($post_id, '_social_score', $social_score);

to this:

update_post_meta($post_id, '_social_score', $social_score);
return $social_score;

Update:

You are using three different variables for the post id, namely: $post_ID, $post_id and $post->ID. You should use the function argument: $post_ID.

3
  • Within a function or method, the return statement immediately stops the execution of the earlier. Commented Apr 1, 2013 at 17:51
  • Thanks, birgire and Johannes. I tried the code below, but it still does not add or update the post_meta in the DB. I guess I am missing something, but I am not sure what that would be. :( Commented Apr 2, 2013 at 4:09
  • You can try the updated the answer.
    – birgire
    Commented Apr 2, 2013 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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