Home › Forums › General Issues › Expire post to draft › Reply To: Expire post to draft
You could just create a function that loops through all your posts and checks the time, and if expired updates the post status to draft. Run it on cron to keep it from killing performance.
if (!wp_next_scheduled('expire_posts')){
wp_schedule_event(time(), 'daily', 'expire_posts');
}
add_action('expire_posts', 'expire_posts_function');
function expire_posts_function() {
$today = time();
$args = array(
'post_type' => array('post'), // array of the post types you want to check
'posts_per_page' => -1 // get all the posts
);
$posts = get_posts($args);
foreach($posts as $p){
if(get_field('expiration', $p->ID) > $today){
$postdata = array(
'ID' => $p->ID,
'post_status' => 'draft'
);
wp_update_post($postdata);
}
}
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.