Skip to content

Instantly share code, notes, and snippets.

@dgoze
Created July 24, 2024 18:06
Show Gist options
  • Save dgoze/ba500abc52ad192c2f2023217f3e1e40 to your computer and use it in GitHub Desktop.
Save dgoze/ba500abc52ad192c2f2023217f3e1e40 to your computer and use it in GitHub Desktop.
Move Old Events to 'Past Event' Taxonomy #wordpress #cron
<?php //for formatting - ignore this line
// Move Old Events to 'Past Event' Taxonomy
add_action( 'hide_old_events', 'run_hide_old_events' );
add_action('admin_init', function() { // check on admin load, could be on init, but seems overkill
if (! wp_next_scheduled ( 'hide_old_events' )) { //see if the event is set
wp_schedule_event( strtotime('tomorrow 12:01am'), 'daily', 'hide_old_events' ); //Schedule it to run at 12:01 am - adjust to your liking
}
});
function run_hide_old_events() {
apply_filters("simple_history_log", "Hide old events started"); //log it in Simple History plugin
$today = current_time('Ymd');
$tag = 'Past Events'; //Taxonomy term
$tax = 'event_category'; //Taxonomy slug
$type = 'events'; //CPT slug
$args = array (
'post_type' => $type,
'meta_key' => 'event_end_date', //ACF date field field
'meta_compare' =>'<',
'meta_value' => $today,
'posts_per_page' => -1,
);
$posts = get_posts( $args );
if(!empty($posts))
{
foreach($posts as $post)
{
wp_set_post_terms( $post->ID, $tag,$tax, true );
apply_filters("simple_history_log", "Hide old event :". $post->post_title . " on: " . $today); //log it in Simple History plugin
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment