Post Duplicating When I run my code to change status

Hi

I have written some code that loops through the published posts and when the Expiry Date (new field I added) is equal to today’s date, the post status is changed to draft.

It does change the status to draft however it duplicates the post. So I end up with a post set to draft and another one that is published.

Below is a sample of my code:

<?php
    $args  = array ("post_type" => "post", "post_status" => "publish");
    $post_query = new WP_Query($args);

   if ($post_query->have_posts()) 
   {
  	while($post_query->have_posts()) 
	{
	    $post_query->the_post();
            $sExpiry = get_post_meta (get_the_ID(), "meta-text", true);
	    $sToday = date("Y-m-d");

	    if ($sExpiry == $sToday)
	    {
	       echo "$sExpiry<br>";
	       echo "$sToday<br><br>";
	       $my_post = array ("ID" => the_ID(), "post_status"  => "Draft",);
	       wp_update_post ($my_post);	       
	    }
	}
   }
?>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.