'Wordpress: give a post a taxonomy if the name is smaller then date

The custom post type has the name/title with date: yyyy-mm-dd-name.

I would like to check every hour, if the date is "bigger/newer" then in the title. If yes, a taxonomy should be added.

I tried without success: (i can see the scheduled event is listed, but nothing else happens)

// a custom hook to schedule
add_action( 'check4pastevents', 'check4pastevents_cats' );

// make sure the event hasn't been scheduled
if( ! wp_next_scheduled( 'check4pastevents' ) ) {
    // Schedule the event
    wp_schedule_event( time(), 'hourly', 'check4pastevents' );
}

function check4pastevents_cats() {

    //fetch all which have no assigned term in 'topicality' to assign 'past-event'
    $args = array('post_type'       => 'events',
                  'posts_per_page'  => -1,
                  'tax_query'       => array(
                      array(
                          'taxonomy'            => 'topicality',
                          'field'               => 'slug',
                          'terms'               => 'past-event',
                          'operator'            => 'NOT IN',
                      ),
                  ),
    );
    //          'terms'         => get_terms( 'past-event', array( 'fields' => 'ids'  ) ),
    
    // get all posts with the required category
    $myposts = get_posts( $args );

    // loop through all posts and update with new tax
    foreach ( $myposts as $mypost ) {
        
        $date_now   = new DateTime("Y-m-d-H-i");
        $date_event = new DateTime(substr($mypost->name, 0, 16));

        if ($date_event > $date_now) {
            $args = array(
                'ID'            => $mypost->ID,
                'tax_query'     => array(
                    array(
                        'taxonomy'  => 'topicality',
                        'terms'     => 'past-events',
                    )
                )
            );
            wp_update_post($args);
        }
        
    }   
}

Does anybody see something wrong i did? Thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source