I have created a function to change the post category to archive after expiry date. And it will update every time when I save a post.
But the question is that how can I schedule an update for posts daily. Otherwise, the post will show in the frontend with outdated information.
It would be great if anyone can help me. Thanks a lot!
// Archive post after expired
if ($expireTransient = get_transient($post->ID) === false) {
set_transient($post->ID, 'set for 1 minutes', 1 * MINUTE_IN_SECONDS );
$today = date('d/m/Y');
$args = array(
'post_type' => 'post',
'posts_per_page' => 200,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'end_date',
'value' => $today,
'compare' => '<='
)
)
);
$posts = get_posts($args);
foreach( $posts as $post ) {
if(get_field('end_date', $post->ID)) {
$postdata = array(
'ID' => $post->ID,
'post_category' => array(176)
);
wp_update_post($postdata);
}
}
}