This code works perfectly!
function delete_expired_posts() {
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'fecha_borrado',
'compare' => 'EXISTS',
),
),
'posts_per_page' => -1,
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$acf_date = get_field('fecha_borrado', $post_id);
$acf_timestamp = strtotime($acf_date);
$current_timestamp = time();
if ($acf_timestamp && $acf_timestamp < $current_timestamp) {
wp_delete_post($post_id, true); // Permanently delete post
}
}
}
wp_reset_postdata();
}
if (!wp_next_scheduled('delete_expired_posts_daily')) {
wp_schedule_event(time(), 'hourly', 'delete_expired_posts_daily');
}
add_action('delete_expired_posts_daily', 'delete_expired_posts');