1

Ok, so here is a slightly weird one that I could not find an answer to... yet.

I've built a classifieds site using WP-Types, but ignore that fact for now.

Is there any way to auto-expire all of an authors' posts all at once on a specific date or after a specific number of days.

1

I'm not away of any expired post status, but you can set the post's status to draft of trash.

First, you need to set up a cron job to run once in a while, and check if the date has come. Finding the proper time is up to you.

// Let's check the scheduled even on WP load
add_action( 'init', 'schedule_post_expiration' );
function schedule_post_expiration() {

    // If the even is not scheduled already, then do schedule it
    if ( ! wp_next_scheduled( 'reset-post-status' ) ) {
        wp_schedule_event( strtotime( '00:00:00' ), 'daily', 'reset-post-status' );
    }

}

// We hook our callback function to our custom action hook to run
// daily and update the post's status
add_action( 'reset-post-status', 'reset_post_status_callback' );
function reset_post_status_callback(){

    // Get the current date
    $date = date( 'Y-m-d' );

    // Set the date we want to check. It should match the above date format.
    $check_date = '2018-02-11';

    // Check if the time has come
    if ( strtotime( $date ) > strtotime( $check_date ) ){

        // Query a list of a specific author's posts
        $author_args = array( 'author' => 123, 'post_status' => 'publish' );

        // Do the query
        $author_query = new WP_Query( $author_args );

        // Check if the query has any post, and setup the post data
        if ( $author_query->have_posts() ) {
            while ( $author_query->have_posts() {
                $author_query->the_post();

                // Update the post's status
                wp_update_post( array( 'ID' => get_the_ID(), 'post_status' => 'trash' ) );

            }
        }
        // Reset the posts data
        wp_reset_postdata();
    }

}
  • Thanks very much @jack-johansson I think this will do that trick. Think I am going to try and see if this solution can be developed into a plugin or something so that I can simply enter the date on a user's profile. – labworxsa Feb 20 '18 at 6:57

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.