0

I'd like somer code that I can place in my functions.php file that will only modify the specified custom post type.

I have already tried several plugins, but all of them have more features than I need. It's overkill to mess with things I don't want them to mess with.

Ideally, the code will:

  • Set up a daily cron job for 00:00 (at night)
  • Fire a custom function that queries the database for the custom post type and compares its publication date with current date.
  • If the custom post type is older than 3 months (the custom function returns true), then it will call wp_delete_post() for the post's ID.

Unfortunately, I can't put it all together myself.

Is there a good (simple) plugin that does this? A code snippet you have off the top of your head?

2 Answers 2

2
add_action( 'wp', 'delete_expired_coupons_daily' );
function delete_expired_coupons_daily() {
if ( ! wp_next_scheduled( 'delete_expired_coupons' ) ) {
    wp_schedule_event( time(), 'daily', 'delete_expired_coupons');
 }
}
add_action( 'delete_expired_coupons', 'delete_expired_coupons_callback' );
function delete_expired_coupons_callback() {
   $args = array(
    'post_type' => 'coupon',
    'posts_per_page' => -1
);

$coupons = new WP_Query($args);
if ($coupons->have_posts()):
    while($coupons->have_posts()): $coupons->the_post();  
       // get the post publish date  
       $test = get_the_date();
       // convert it to strto 
       $converter = strtotime($test);
       // add 3 months to it
       $deletedate = date(strtotime("+3 month", $converter));
       // check if today is more than 3 months of publish date
        if (time() > $deletedate) {
            wp_delete_post(get_the_ID());
            //Use wp_delete_post(get_the_ID(),true) to delete the post from the trash too.                  
        }

    endwhile;
endif; 
}

Put it in the functions.php. This in theory would do the job. Copied from Posts to expire (deleted) after a date and changed the comparison.

Change the increment to 3 mins/seconds for testing by

     $test = get_the_date('F j, Y G:i:s');
     $converter = strtotime($test);
     $deletedate = date(strtotime("+3 minutes", $converter));

Use this plugin to test it. https://wordpress.org/plugins/trigger-scheduled-events/

1

Well there is two ways of doing it in that i can think of...

The simplest seems to be the wp_cron command.

http://codex.wordpress.org/Function_Reference/wp_cron

And in your functions you can add some custom SQL (or other) to update your database with whatever you want...

Alternatively you can try this plugin http://wordpress.org/extend/plugins/wp-cron-control/ And make it do you want.

I can't provide the excact SQL you need but with these two you should be able to run scheduled unattended events on your site.

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.