Archived topic
Change post status to Draft based on date in custom field
4 replies · Started by John on December 20, 2022
Hi team,
I've been struggling with this issue for a while. I have a custom post type (CPT) event, and I need to display the upcoming events only. I don't want past events to accumulate and 'clog' the website for visitors.
Fernando graciously helped me with a code snippet to filter a GB Pro Query Loop, but I realized that it was only a partial answer. The past events still show up in several Archive pages (or direct URL linked from outside). So instead of filtering them at the Query Loop level, I figured it's much cleaner to remove them from public view entirely.
So I inquired into changing the Status of a post from "Published" to "Draft" based on a date from a custom field, e.g. event_end_date. After multiple attempts, I am working with the code snippet below, but it's still not working for whatever reason. Now I'm wondering if the snippet is conflicting with any of the theme. I'm using GP Premium.
// expire events based on date field.
if (!wp_next_scheduled('hol_expire_events')){
wp_schedule_event(time(), 'daily', 'hol_expire_events'); // this can be hourly, twicedaily, or daily
}
add_action('hol_expire_events', 'hol_expire_events_function');
function hol_expire_events_function() {
$today = date('Y-m-d');
$args = array(
'post_type' => array('event'), // post types you want to check
'posts_per_page' => -1
);
$posts = get_posts($args);
foreach($posts as $p){
$expiredate = get_field('event_end_date', $p->ID, false, false); // get the raw date from the db
if ($expiredate) {
if($expiredate < $today){
$postdata = array(
'ID' => $p->ID,
'post_status' => 'draft'
);
wp_update_post($postdata);
}
}
}
}
I'll add that I suspected it may not work due to the date format. Although I don't know how to check whether it's the culprit, I did make sure that the custom field outputs Y-m-d.
I was also wondering if maybe it's a cron-related issue, so I installed the WP Crontrol plugin and I'm able to see that the cron is running hol_expire_events_function(), although it says it's running hourly, and I would expect it daily based on the snippet.
Thank you for any help.
Hi John,
I could be wrong here but I don't believe this question is related to GP at all as the theme (any themes) do not handle any of the items mentioned above.
Does the same solution work in a Twenty series WP theme environment?
I would highly recommend posting in a WP general forum for a WP general question like this:
https://wordpress.stackexchange.com/
Let me know :)
Hi Leo,
Thank you for your response. You may be right, unfortunately I'm not PHP-savvy or Wordpress Theme-savvy enough to know whether the theme is involved or not. I did post on StackExchange and this snippet is the suggestion I got. The author certifies it's working for him, so I figured it may not work for me due to the specific theme I'm using.
I don't know if this snippet works with another theme, I'm a bit afraid to switch themes and break things as I've developed a lot of the website relying on GeneratePress features.
I’m a bit afraid to switch themes and break things as I’ve developed a lot of the website relying on GeneratePress features.
It would be good to start a staging site, or even just a fresh install using a default Twenty series WP theme to test the code first.
Nothing in the code suggested that it should be theme-specific.
Thank you, I will take your word for it and assume it's not