I have this cron set up to trash posts x days after it is posted. This works. Edit: Added my answer to my question.

add_action( 'wp', 'do_trash_ads' );
function do_trash_ads()
{
    if ( ! wp_next_scheduled( 'delete_classifieds' ) )
        wp_schedule_event( time(), 'daily', 'delete_classifieds' );
}

add_action( 'delete_classifieds', 'expire_posts' );
function expire_posts()
{
    global $wpdb;
    $daystogo = "14";

    $post_ids = $wpdb->get_results( "
        SELECT ID 
        FROM {$wpdb->posts}
        WHERE post_type="classifieds" 
        AND post_status="publish" 
        AND DATEDIFF(NOW(), post_date) > '{$daystogo}'
    " );
    foreach( $post_ids as $id )
    {
        $postid =  $id->ID;

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

What I would like to do: Include posts in the above function that are based on a meta field value (21 days is the default, but a user can select an earlier date).

I set up a 2nd cron to do this.

add_action( 'wp', 'do_trash_ads_user' );
function do_trash_ads_user()
{
    if ( ! wp_next_scheduled( 'delete_ads_user' ) )
        wp_schedule_event( time(), 'daily', 'delete_ads_user' );
}

add_action( 'delete_ads_user', 'expire_posts_user' );
function expire_posts_user()
{
   global $wpdb;

   $post_ids = $wpdb->get_results( "
      SELECT ID 
      FROM {$wpdb->posts}
      WHERE post_type="classifieds" 
      AND post_status="publish"
   " );

   foreach( $post_ids as $id )
   {
       $postid =  $id->ID;
       $expiration_value = get_post_meta( $postid, 'ecpt_ad-expire-date', true );

       if( $expiration_value )
       {
           $todays_date = date( "Y-m-d" );
           $today = strtotime( $todays_date );
           $expiration_date = strtotime( $expiration_value );
           if ( $expiration_date > $today )
           { 

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

               wp_update_post( $my_post );
           }
        }
    }
}

I don’t know if this is the best method but it is working.

1 Answer
1

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 );
           }

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *