Reputation: 761
I want to run a function that will update all posts. My problem is that the function only runs when I visit that specific post (Only the specific post will be updated).
My function updates Facebook likes from the post source URL
function update_facebook_likes($content) {
global $post;
$url_path = get_post_meta(get_the_ID(), 'url_source', TRUE);
$data = file_get_contents('https://graph.facebook.com/v2.2/?id='.$url_path.'&fields=share&access_token=****');
$obj = json_decode($data);
$like_na = $obj->{'share'};
$like_no = $like_na->{'share_count'};
update_post_meta($post->ID, 'fb_likes_count', $like_no);
}
add_action('wp', 'update_facebook_likes');
function display_fb_likes() {
global $post;
$fb_likes_count = get_post_meta($post->ID, 'fb_likes_count', true);
echo $fb_likes_count;
}
Upvotes: 0
Views: 2374
Reputation: 1909
Try to use this foreach loop to update all posts. using get_posts()
function
https://developer.wordpress.org/reference/functions/get_posts/
$args = array(
'posts_per_page' => -1,
);
$posts_array = get_posts( $args );
foreach($posts_array as $post_array)
{
update_post_meta($post_array->ID, 'fb_likes_count', true);
}
Upvotes: 2