- This topic has 4 replies, 2 voices, and was last updated 3 years, 4 months ago by
John.
-
AuthorPosts
-
December 20, 2022 at 11:41 am #2468568
John
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.
December 20, 2022 at 1:22 pm #2468656Leo
StaffCustomer SupportHi 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 š
December 20, 2022 at 2:25 pm #2468711John
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.
December 20, 2022 at 2:31 pm #2468713Leo
StaffCustomer SupportIā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.
December 20, 2022 at 2:34 pm #2468718John
Thank you, I will take your word for it and assume it’s not
-
AuthorPosts
- The topic ‘Change post status to Draft based on date in custom field’ is closed to new replies.