plugins - Draft standard post by ACF Data picker

admin2025-01-08  6

I have a custom field named "fecha_borrado". The date is a date picker from Advanced Custom Field.

I have this code but don't work. Maybe i did some wrong?

I added too define('ALTERNATE_WP_CRON', true); in my wp-config file

// expire offer posts on date field.
if (!wp_next_scheduled('expire_posts')){
  wp_schedule_event(time(), 'hourly', 'expire_posts'); // this can be hourly, twicedaily, or daily
}

add_action('expire_posts', 'expire_posts_function');

function expire_posts_function() {
    $today = date('Ymd');
    $args = array(
        'post_type' => array('post'), // post types you want to check
        'posts_per_page' => -1 
    );
    $posts = get_posts($args);
    foreach($posts as $p){
        $expiredate = get_field('fecha_borrado', $p->ID, false, false); // get the raw date from the db
        if ($expiredate) {
            if($expiredate < $today){
                $postdata = array(
                    'ID' => $p->ID,
                    'post_status' => 'draft'
                );
                wp_update_post($postdata);
            }
        }
    }
}

I have a custom field named "fecha_borrado". The date is a date picker from Advanced Custom Field.

I have this code but don't work. Maybe i did some wrong?

I added too define('ALTERNATE_WP_CRON', true); in my wp-config file

// expire offer posts on date field.
if (!wp_next_scheduled('expire_posts')){
  wp_schedule_event(time(), 'hourly', 'expire_posts'); // this can be hourly, twicedaily, or daily
}

add_action('expire_posts', 'expire_posts_function');

function expire_posts_function() {
    $today = date('Ymd');
    $args = array(
        'post_type' => array('post'), // post types you want to check
        'posts_per_page' => -1 
    );
    $posts = get_posts($args);
    foreach($posts as $p){
        $expiredate = get_field('fecha_borrado', $p->ID, false, false); // get the raw date from the db
        if ($expiredate) {
            if($expiredate < $today){
                $postdata = array(
                    'ID' => $p->ID,
                    'post_status' => 'draft'
                );
                wp_update_post($postdata);
            }
        }
    }
}
Share Improve this question asked Nov 13, 2024 at 11:50 vektorvektor 212 bronze badges 3
  • 1 you can't compare dates using strings, if ( "2024/01/01" < "2025/01/01" ) { doesn't work. The reason some code can do this is because it's handling dates as numbers e.g. seconds since the Unix epoch, not as strings. The code in your question won't scale anyway, it's easier and simpler to do this using meta_query so that MySQL does the date comparison, and to avoid setting posts_per_page to -1 as it means this feature will break once you publish more than a few hundred posts. You might also want to specify the published post status explicitly – Tom J Nowell Commented Nov 13, 2024 at 12:18
  • 1 also, the format you store the date in matters if you're going to have MySQL do the comparison. How you'd compare a date that's a string is a general PHP problem rather than a WordPress problem – Tom J Nowell Commented Nov 13, 2024 at 12:19
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Bot Commented Nov 13, 2024 at 18:55
Add a comment  | 

1 Answer 1

Reset to default 2

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');
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270965a1475.html

最新回复(0)