0

How can I delete the posts one by one depending on the published time of the post every 24 hour?

Why this snippet deleting all the posts at once, even if they are not published in the same hour or day?

I'm highly appreciated your help.

add_action( 'wp', 'delete_expired_story_daily' );
function delete_expired_story_daily() {
    if ( ! wp_next_scheduled( 'delete_expired_story' ) ) {
        wp_schedule_event( time(), 'daily', 'delete_expired_story');
    }
}

Expired callback.

add_action( 'delete_expired_story', 'delete_expired_story_callback' );

function delete_expired_story_callback() {
    $args = array(
        'post_type' => 'post',
        'category_name' => 'stories',
        'posts_per_page' => -1
    );

    $stories = new WP_Query($args);
    if ($stories->have_posts()):
        while($stories->have_posts()): $stories->the_post();    

            $expiration_date = get_post_meta( get_the_ID(), 'expiry_story_date', true );
            $expiration_date_time = strtotime($expiration_date);

            if ($expiration_date_time < time()) {
                wp_delete_post(get_the_ID(),true);                 
            }

        endwhile;
    endif;
}

Update 1

I tried to use the timestamp but still not working?

I added the published time + 86400 using the timestamp!

add_action( 'wp', 'delete_expired_story_daily' );
function delete_expired_story_daily() {
    if ( ! wp_next_scheduled( 'delete_expired_story' ) ) {
        wp_schedule_event( time(), 'daily', 'delete_expired_story');
    }
}

add_action( 'delete_expired_story', 'delete_expired_story_callback' );

function delete_expired_story_callback() {
    $args = array(
    'post_type' => 'post',
    'category_name' => 'stories',
    'post_status' => 'publish',
    'posts_per_page' => -1
    );

    $stories = new WP_Query($args);
    if ($stories->have_posts()):
        while($stories->have_posts()): $stories->the_post();    

    $publish_time = get_the_time('U'); // Returns our $publish time as a Unix timestamp
    $delete_time = $publish_time + 86400; // 60 sec * 60 min * 12 hrs = 43,200 sec
    $current_time = time(); // time is a the current time in a Unix timestamp

    if ( $current_time >= $delete_time ) {
        wp_delete_post(get_the_ID(), true);
    }

        endwhile;
    endif;
}

1 Answer 1

0

Let me answer your questions starting from the second one... So...

Why is it deleting all posts at once?

You scheduled you event correctly, so it will run once a day. And in the function that is run, you select some posts and delete them, if they’re expired:

$args = array(
    'post_type' => 'post',
    'category_name' => 'stories',
    'posts_per_page' => -1
);

$stories = new WP_Query($args);

The code above will select all posts (because posts_per_page = -1) from “stories” category.

And here you loop through all of these posts, check if given post has expired and delete it if so:

    while($stories->have_posts()): $stories->the_post();    

        $expiration_date = get_post_meta( get_the_ID(), 'expiry_story_date', true );
        $expiration_date_time = strtotime($expiration_date);

        if ($expiration_date_time < time()) {
            wp_delete_post(get_the_ID(),true);                 
        }

    endwhile;

That’s why it deletes all posts at once.

So how to make it delete only one post?

The easiest way to modify your code is to add one line that will stop the loop after first post gets deleted:

        if ($expiration_date_time < time()) {
            wp_delete_post(get_the_ID(),true);    
            return;             
        }

But of course this is not the nicest way to do this.

Much better approach would be to get only the posts that should be deleted and get only one of them:

$args = array(
    'post_type' => 'post',
    'category_name' => 'stories',
    'posts_per_page' => 1,
    'meta_query' => array(
        array( 'key' => 'expiry_story_date', 'compare' => '<=', 'value' => date('Y-m-d') ) // here’s the very important part - you have to change the date format so it’s the same as the format you store in your custom fields
    )
);

$expired_stories = new WP_Query($args);
3
  • Thank you @Krzysiek, But I have a list for all stories category posts on the front end with ajax pagination! So I don't need to show only one post, I just want to delete the post who have get 24 hours depend on post publish time, So, how can I do that? Apr 26, 2019 at 13:08
  • Kindly check the updated question, but not working! Thank you. Apr 26, 2019 at 13:20
  • Hello @KrzysiekDróżdż♦ any hope to make it work, Thank you! Apr 27, 2019 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.