mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
36 lines
793 B
Text
36 lines
793 B
Text
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' => 'expiry_date',
|
|
'value' => current_time( 'Y-m-d' ),
|
|
'compare' => '<='
|
|
),
|
|
array(
|
|
'key' => 'expiry_date',
|
|
'value' => '',
|
|
'compare' => '!='
|
|
)
|
|
)
|
|
);
|
|
|
|
$coupons = get_posts( $args );
|
|
|
|
if ( ! empty( $coupons ) ) {
|
|
$current_time = current_time( 'timestamp' );
|
|
|
|
foreach ( $coupons as $coupon ) {
|
|
wp_trash_post( $coupon->ID );
|
|
}
|
|
}
|
|
}
|
|
add_action( 'delete_expired_coupons', 'delete_expired_coupons' );
|