How to create pages that do change or expire after a specified amount of time?

The question:

You can create a new page/post for this and set a custom publish date for it, but
now the question is:

How can you make a page to be available for a specified amount of time and replace it with something else after this?

One usage example would be if you want to do a limited time offer and after this you want to replace or hide the page.

As you can imagine I’m looking for a solution that would allow you schedule this, so you could be in vacation when the change happens 😉

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

If you made your special offers a custom post type you could set the ‘scheduled’ date to the day you wanted it to expire then use this code to auto publish it on schedule after you’ve changed the scheduled date. The ‘future_show’ line goes with my ‘show’ custom post type so if you had a ‘deal’ custom post type it would say ‘future_deal’.

function sfn_setup_future_hook() {
 // Replace native future_post function with replacement

function publish_future_post_now($id) {

add_action('init', 'sfn_setup_future_hook');

You can then set the post to expire if it’s older than today on the server with the code below. It hooks into WordPress cron so you’re not running it all the time but you’ll notice a line commented out that runs it on init so you can test it. For testing comment out the first 3 lines and use the init action. Don’t do this on production though.

// setting posts with current date or older to draft
if (!wp_next_scheduled('sfn_expire_hook')){
    wp_schedule_event( time(), 'hourly', 'sfn_expire_hook');
add_action( 'sfn_expire_hook', 'sfn_show_expire');
function sfn_show_expire(){
    global $wpdb;
    $server_time = date('mdy');
    $result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'show' AND post_status = 'publish'");
    if( !empty($result)) foreach ($result as $a){
        $show_time = get_the_time('mdy', $a->ID);
        if ( $server_time > $show_time ){
           $my_post = array();
           $my_post['ID'] = $a->ID;
           $my_post['post_status'] = 'draft';
           wp_update_post( $my_post );
    } // end foreach
// add_action( 'init', 'sfn_show_expire' );

Don’t forget to change my function prefix (sfn) to your own just to sandbox it’s function from anything else.

Edit I just also thought that you could change the SQL query to grab posts in a category of ‘deal’ and it would work the same way. Creating an entire post type then including it in the site/blog may be more work than is needed.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment