How to automatically compare date from custom post type with current date and change taxonomy based on a result?

This is pretty straight forward and easy to do with $wpdb and SQL statement

the relationship between any post type (custom or not) and taxonomy term (custom or not ) are stored wp_term_relationships table which has object_id and term_id

all you have to do is query the record that has the term_id for active1 term and update that to term_id of past1 term, if the postmeta value with meta key datum_polaska of the object_id is greater than the current time.

Here is a function to do that (untested)

function _update_tours_post_type_terms() {
    // Added additional condition to terminate the function when called many times per day
    $tsKey = '_update_tours_post_type_terms_last_run';
    $lastRun = get_transient( $tsKey ); // get the transient data
    /* Un-comment the line below if you want to;
     * stop execution if the last time it was called is < 12 hour ( 12 hr = 43200 seconds )

    //if (  $lastRun + 43200 > time() )
    global $wpdb;

    $activeTermID = 1; // set the active1 term ID   
    $pastTermID = 2; // set the past1 term ID
    $currentDate =  date('Y-m-d'); // get current date and make sure it matches the format you stored in postmeta

        Next code explains below per line
        -run update statement on term_relationships table on all rows matching all if condition below
        -set the term_taxonomy_id to $pastTermID
        -load the postmeta where meta post_id = object_id and meta_key is datum_polaska
        -if the current term_taxonomy_id value is $activeTermID
        -if datum_polaska is less than $currentDate
        UPDATE $wpdb->term_relationships tr         

        SET tr.term_taxonomy_id = $pastTermID

        INNER JOIN $wpdb->postmeta dp ON dp.post_id = tr.object_id AND dp.meta_key = 'datum_polaska'        

        WHERE tr.term_taxonomy_id = $activeTermID

        AND dp.meta_value < '$currentDate'


    // Update transient to current time if the function runs
    set_transient( $tsKey, time() );


and thats it, no need for query or looping, this just need the post type to to have the active1 term set while past1 should not be assign, otherwise it wont work as this simply update/replaces the record already present in the database.

All you have to do is call that function in your wp_schedule_event

Optional setting up cron from your server instead of using wordpress event scheduler

You may also set-up a cron directly from your server that runs every day.
all you have to do is set a url param and call the function when the param is present in the request.


add_action('init', function() {
    //terminate if update_tours_post_type_terms is not present in url parameter or if its value is not equal to 1
    if ( !isset( $_REQUEST['update_tours_post_type_terms'] ) || !$_REQUEST['update_tours_post_type_terms'] === '1' )

Then you can simply open the URL to run that function

and setup a daily cron on your server to ping that URL every day at 1AM

0 1 * * * wget -O - >/dev/null 2>&1


If you want to test the function, first make sure the code below is commented (I already edited the function above which disable those lines

if (  $lastRun + 43200 > time() )

Second, you can implement the init hook above then simply open the example url I mentioned

OR just hook the function direclty on init (this should not be done unless you are just testing)


add_action('init', '_update_tours_post_type_terms');

then wherever you browse your site (front-end or back-end) that function will run,

you can simply assign an active1 term with a past date meta, and it should update itself to past1 once you hit save