I have seen some examples of people asking similar to this but no real solution. Hopefully someone maybe able to point me in the right direction
I have a Custom Post Type set up called featured_product
. Within this I have set up a custom date/time field called featured_product_expiration
.
What I'm trying to achieve is once the expiration date of the post type has expired, the post would be moved to Trash or set to Draft (so that it no longer shows on the front end).
I have set up WP Crontrol plugin which is using a hook to target a function in my functions.php. The cron is set to run every minute.
This is my function in functions.php (the featured_product_action
is what I have called my custom cron job in WP Crontrol)
add_action('featured_product_action', '_delete_expired_fp');
$past = strtotime( "- 1 day" );
function _delete_expired_fp() {
// Set our query arguments
$args = [
'fields' => 'ids', // Only get post ID's to improve performance
'post_type' => 'featured_product', // Post type
'posts_per_page' => -1,
'meta_query' => [
[
'key' => 'featured_product_expiration', // Replace this with the event end date meta key.
'value' => $past,
'compare' => '<='
]
]
];
$q = get_posts( $args );
// Check if we have posts to delete, if not, return false
if ( !$q ) {
return false;
}
// OK, we have posts to delete, lets delete them
foreach ( $q as $id ){
wp_trash_post( $id );
}
}
I can see in the Dashboard the the cron is running correctly and referring to the correct callback function but nothing is happening on the front or back end. Is anybody able to pinpoint where I maybe going wrong with this. I feel like I'm nearly there.
Could the issue be related to the way I have my date formatted in my custom date/time field?
Any help much appreciated.