Updating a custom post status after an expiry date rather than trashing it

I have set up a custom post type of featured products. Within that post type I have a custom date field for an expiry date. Currently when the date expires, the post gets moved to the Trash. I'm looking to change this though and hoped somebody maybe able to help.

My current function is:

function _delete_expired_fp() {
  $args = array(
      'post_type' = 'featured_product',
      'posts_per_page' = -1
  );

  $fproducts = new WP_Query($args);
  if ($fproducts-have_posts()):
      while($fproducts-have_posts()): $fproducts-the_post();    

          $expiration_date = get_post_meta( get_the_ID(), 'featured_product_expiration', true );
          $expiration_date_time = strtotime($expiration_date);

          if ($expiration_date_time  time()) {
              wp_trash_post(get_the_ID());
                         
          }

      endwhile;
  endif;
}

What I'd like to happen is rather than send the expired post to the trash, I would like to change it's status to Draft. I believe I can use something like wp_update_post but I'm not too sure how to implement this into my existing code as I'm sure there is more to it.

Any help much appreciated.

*EDIT

wp_update_post( array(
                'ID'            = $post-ID,
                'post_status'   = 'draft',
            ) );

Could something like this work?

Topic wp-update-post custom-field draft custom-post-types Wordpress

Category Web

About

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