admin管理员组

文章数量:1429611

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;
}

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;
}
Share Improve this question edited Apr 26, 2019 at 20:13 Adham Mohamed asked Apr 25, 2019 at 19:40 Adham MohamedAdham Mohamed 1853 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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);

本文标签: phpAutomatically delete posts one by one depending on published time