-
-
Save lukecav/a738a13ab72e7da8beb4f99b26847aa2 to your computer and use it in GitHub Desktop.
functions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Schedule the daily event if necessary. | |
*/ | |
function schedule_delete_expired_coupons() { | |
if ( ! wp_next_scheduled( 'delete_expired_coupons' ) ) { | |
wp_schedule_event( time(), 'daily', 'delete_expired_coupons' ); | |
} | |
} | |
add_action( 'init', 'schedule_delete_expired_coupons' ); | |
/** | |
* Trash all expired coupons when the event is triggered. | |
*/ | |
function delete_expired_coupons() { | |
$args = array( | |
'posts_per_page' => -1, | |
'post_type' => 'shop_coupon', | |
'post_status' => 'publish', | |
'meta_query' => array( | |
'relation' => 'AND', | |
array( | |
'key' => 'date_expires', | |
'value' => current_time( 'timestamp' ), | |
'compare' => '<=' | |
), | |
array( | |
'key' => 'date_expires', | |
'value' => '', | |
'compare' => '!=' | |
) | |
) | |
); | |
$coupons = get_posts( $args ); | |
if ( ! empty( $coupons ) ) { | |
foreach ( $coupons as $coupon ) { | |
wp_trash_post( $coupon->ID ); | |
} | |
} | |
} | |
add_action( 'delete_expired_coupons', 'delete_expired_coupons' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment