Home › Forums › ACF PRO › Expire Posts on Datepicker Field › Reply To: Expire Posts on Datepicker Field
Hey Gents,
I’ve attempted to use both code snippets provided by @jonathan & @precisioncreations but without much luck. I’ve added both to my functions file (at different times) with the only difference being my post type and key value based on my client’s setup.
I can confirm the meta_value shows as a previous date of “20161130” in the database, yet the posts remain in a published state.
When first attempting to force the cron job manually through BackupBuddy’s server tools, I received the following error message from the method @jonathan provided:
“Fatal error: Call to a member function have_posts() on a non-object in /path/functions/custom.php on line 220″
Line 220 is:
if( $posts->have_posts() ){
I removed the if call from the code altogether and was able to manually run the cron job, but still with the same result of no posts updating.
Below is the code I’m using in the site’s functions file:
// Expire Post Settings
if (!wp_next_scheduled('expire_posts')){
wp_schedule_event(time(), 'hourly', 'expire_posts');
}
add_action('expire_posts', 'expire_posts_function');
function expire_posts_function() {
$today = time('Ymd');
$args = array(
'post_type' => 'ivy_event',
'posts_per_page' => 200,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'expire_post_date',
'value' => $today,
'compare' => '<='
)
)
);
$post_query = new WP_Query($args);
while( $post_query->have_posts() ){
$post_query->the_post();
wp_transition_post_status('draft', 'publish', $post);
}
wp_reset_postdata();
}
Any help here would be greatly appreciated. I’m sure it’s something simple, but I’m on the tail end of a week where 14 hour shifts have been the norm.
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.