0

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?

2
  • You’d also have to make sure your new status wasn’t returned by the query, else you’d keep reapplying the new status, or filter that out done other way. But otherwise wp_update_post should work fine yes.
    – Rup
    Apr 5, 2021 at 14:31
  • Thank you for the quick reply. So could I use something like the above *EDIT ( I have edited my original post)
    – Adam
    Apr 5, 2021 at 14:41

1 Answer 1

0

Here is the solution I found to update post status based on a date:

Change a Post's Status based on the date from a custom field? (for Event posts)

2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – cjbj
    Feb 16, 2023 at 13:45
  • 1
    If this is answered by another question on this site then you could flag this question as a duplicate of that one.
    – Rup
    Feb 22, 2023 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.