Change post status based on meta value

Your code looks okay, and considering you are not submitting user entered data, the prepare() method isn’t required, but as a matter of best-practice it’s good to learn how it works and use it consistently.

With that said, using the prepare() method, your code would look like this:

$sql = $wpdb->prepare( "
  SELECT ID
  FROM %s
  WHERE post_type="classifieds" 
  AND post_status="publish"
  ", $wpdb->posts );

 $post_ids = $wpdb->get_results( $sql, ARRAY_A );

Also, You could shorten your if statement from:

if ( $expiration_date > $today )
           { 

           }
           else
           { 
               $my_post = array();
               $my_post['ID'] = $postid;
               $my_post['post_status'] = 'trash';

               wp_update_post( $my_post );
           }

to:

if ( $expiration_date < $today )
           { 
               $my_post = array();
               $my_post['ID'] = $postid;
               $my_post['post_status'] = 'trash';
               wp_update_post( $my_post );
           }

Leave a Comment