Home › Forums › ACF PRO › Expire Posts on Datepicker Field › Reply To: Expire Posts on Datepicker Field
Hi @karks88
Almost..
But I would say there’s no need for you to query all posts and then do the check when you can just query all past posts directly and change their statuses..
if (!wp_next_scheduled('expire_posts')){
wp_schedule_event(time(), 'hourly', 'expire_posts'); // running hourly for testing purposes
}
add_action('expire_posts', 'expire_posts_function');
function expire_posts_function() {
$today = time('Ymd'); // matches the date in DB
$args = array(
'post_type' => array('post'), // array of the post types you want to check
'posts_per_page' => 200 // get all the posts but don't set it t o -1 since its a risk of overloading the server
'category' => 'events',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'event_ending_date',
'value' => $today,
'compare' => '<' // you might have to switch this out for >
)
)
);
$post_query = new WP_Query($args);
if( $posts->have_posts() ){
while( $post_query->have_posts() ){
$post_query->the_post();
wp_transition_post_status('draft', 'publish', $post);
}
wp_reset_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.