update post status with cron

Hi I am trying to update the status of a woocommerce order this is what I have at the moment I know the field content matches and if I write this as DB query and run it in phpmyadmin it works can anyone see what is missing for this to work.

It is simply matching the date from the custom field to today I have double checked the format it's correct I've tested the output of the $today=date('j F, Y); and it returns the correct format.

I have tested the cron using. the crontrol plugin it reports the cron has been successfully run.

// Schedule an action if it's not already scheduled
  if ( ! wp_next_scheduled( 'insignia_status_change_cron' ) ) {
wp_schedule_event( time(), 'hourly', 'insignia_status_change_cron' );
  }

 // Hook into that action that'll fire daily
  add_action( 'insignia_status_change_cron', 'insignia_status_change_function' );
  function insignia_status_change_function() {
  $the_query = get_posts( 'post_type=shop_order', 'post_status=wc-on-hold' );   
  foreach($the_query as $single_post) {
    $id=$single_post-ID;
    $the_delivery_date=get_post_meta($id, 'Delivery Date', true );
    if($the_delivery_date!=''){
        $today=date(j F, Y);
        if($the_delivery_date == $today){
            $update_post = array(  
            'ID'            = $id,
            'post_status'   =  'wc-processing',
            'post_type' =  'shop_order' );
            wp_update_post($update_post);
        }   
    }
   }
  }

EDIT 29/06/2021

So I updated the code as per the suggestion in comments to use:

$args = array(
    //'post_type'   = 'shop_order',
    'post_status'       = 'wc-on-hold',
    'meta_value'    =  $today
);

$the_query = wc_get_posts( $args );

Which returns the array but it still doesn't update so I think the problem is with my update section

Topic wp-update-post Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.