1

I am creating a coupon site from scratch. I have reviewed many code examples on the Internet, but have not found anything that will work, as my requirements are little different.

I need help in creating an expiry function that would delete the "coupon" (post_type) automatically.

As a coupon site, all coupons are imported in bulk, so the expiry date format (e.g. 01/12/2014, 01-12-2014, or 1 Dec 2014) differs from coupon to coupon in a particular import (session). This creates a problem when the date format differs, and it's never the same.

Custom field is "expiry_date", it will store the date.

Does any one have a good solution for this problem?

  • You could convert the expiry date into a Unix timestamp strtotime() and then do a simple if to check to if it's higher than time() and then hide otherwise. So it wouldn't show up on the site anymore. Physically deleting it from the database is going to require a daily cron job, not sure if that's really necessary, so long as expired coupons don't show on the site. Is this what you meant? – deflime Jul 6 '14 at 1:27
  • Deleting coupons is required, as for some coupons we have to make some manual changes from the dashboard and i don't want to make any manual changes to the coupon that will be expired. can you provide the code for the whole function. thanks in advance. – user2506619 Jul 6 '14 at 5:53
  • So do you basically just need to be able to tell the difference between expired coupons and active coupons? You could put the expiry date as the Published date for each coupon. Then simply browse the Scheduled filter of your coupons. http://screencast.com/t/E37TWEwU. Then you simply define the post_status parameter of WP_Query() to future. – deflime Jul 6 '14 at 6:26
  • your idea is nice but , i do require that the expired coupons should be deleted from our database. as we will be having thousands of coupon. so using crons will be the better option for us. – user2506619 Jul 6 '14 at 6:39
  • I'm not that familiar with writing cron jobs but there seem to be a lot of resources on Stack Overflow. This isn't really a Worpress question anymore. You just need a cron job to delete MySQL rows as needed, really nothing to do with WP anymore. – deflime Jul 6 '14 at 6:45
6

I did something similar a while ago. I'm not sure deleting a post is a good solution, maybe you can change only the post status to a custom one called Expired for example. But for an example, the code below will delete the post.

First create a daily cron job:

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

Then use the delete_expired_coupons_callback function to loop through the coupon posts, check for the date and delete if necessary:

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

            $expiration_date = get_post_meta( get_the_ID(), 'expiry_date', true );
            $expiration_date_time = strtotime($expiration_date);

            if ($expiration_date_time < time()) {
                wp_delete_post(get_the_ID());
                //Use wp_delete_post(get_the_ID(),true) to delete the post from the trash too.                  
            }

        endwhile;
    endif;
}

Couple of suggestions: When you import the coupons, you can do an strtotime conversion, so the date format is the same in the database. This way you don't need to loop through all of the posts to check the expiration date, you can use a custom query to check if its expired, so the daily job will run much faster:

    $args = array(
        'post_type' => 'coupon',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
               'key' => 'expiry_date',
               'value' => time(),
               'compare' => '<'
            )
        )
    );

You can create an Expiration post status to simply disable these posts without deleting them: http://codex.wordpress.org/Function_Reference/register_post_status

And you can change the post status like this, instead of the wp_delete_post function:

// Update post
$my_post = array();
$my_post['ID'] = get_the_ID();
$my_post['post_status'] = 'expired';

// Update the post into the database
wp_update_post( $my_post );
  • will it work with different time formats. – user2506619 Jul 6 '14 at 11:49
  • The 3 example you showed in the question will work with strtotime, but its not fool-proof. The problem is that 01/12/2014 could be december 1. or january 12, nobody knows 100% sure. – passatgt Jul 6 '14 at 11:51
  • can you explain where should i put this two codes. – user2506619 Jul 6 '14 at 11:53
  • Oh boy...:) You need some experience with WordPress to do something like this. But the code goes inside your theme's function.php file or create a plugin for it. How and where do you registered the coupon post type for the Coupons? – passatgt Jul 6 '14 at 11:55
  • My post_type is created using PODs. i will check the code and let you know. thanks – user2506619 Jul 6 '14 at 13:22
1

I would suggest opening CSV in Excel and use formatting to convert all dates to the same format and save CSV before using this for importing. This should avoid any development efforts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.