我想在不刷新页面的情况下更新post meta:
我有这部分代码:
$api = "https://api.themoviedb.org/3/tv/1402?api_key=########";
$json = file_get_contents($api);
$data = json_decode($json, TRUE);
update_post_meta($post->ID, 'status', $data['status']);
我在post模板中编写此代码。其中通过访问这个页面后元将更新。我想要一种方法来更新后元自动不刷新或访问页面页面。
您检查过wp-cron了吗?你可以创建一个每天或每12小时触发一次的日程表事件?
在这个函数中,你可以查询你的帖子,循环遍历每个帖子来更新帖子元。
可以是这样的:
<?php
if ( ! wp_next_scheduled( 'al_my_scheduled_task' ) ) {
wp_schedule_event( time(), 'hourly', 'al_my_scheduled_task' );
}
add_action( 'al_my_scheduled_task', 'al_my_scheduled_function' );
function al_my_scheduled_function() {
$args = array(
'post_type' => 'posts',
'posts_per_page' => -1,
'limit' => -1
)
$posts = get_posts($args);
if($posts){
foreach($posts as $post){ setup_postdata($post);
$api = "https://api.themoviedb.org/3/tv/1402?api_key=########";
$json = file_get_contents($api);
$data = json_decode($json, TRUE);
update_post_meta( get_the_id() , 'status', $data['status']);
}
wp_reset_postdata();
}
}
?>
此代码未经过测试!请自己测试一下。
请看看这个关于wp-cron和缓存插件问题的答案