4

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.

7
  • 1
    Please read into $wpdb->prepare(), which has several examples here on WP.SE. Than theres also wp_update_post()` and wp_insert_post().
    – kaiser
    Commented Mar 10, 2013 at 23:49
  • @kaiser Oh yes I have been reading lots this weekend. Including several examples here at WP.SE (which is where I got the above code originally) I have another function (a wp query and update post()) to query this meta key in another cron (not tested) but wanted to do it all at once. Should I use $wpdb->prepare my first sql query?
    – Michael B
    Commented Mar 11, 2013 at 0:42
  • 2
    Michel, there're a lot of explanations about the prepare() method that tell you exactly how and when to use it. To avoid duplication I'll let you read the existing answers :)
    – kaiser
    Commented Mar 11, 2013 at 1:18
  • Sorry I hoped I asked a worthy question. I'll figure something out at some point and just go back to lurking.
    – Michael B
    Commented Mar 11, 2013 at 2:22
  • 1
    @kaiser I changed my code. I hope this is a better way. Thanks for making me read deeper. :)
    – Michael B
    Commented Mar 20, 2013 at 4:28

1 Answer 1

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

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.