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

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 😉

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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’.

<?php
function sfn_setup_future_hook() {
 // Replace native future_post function with replacement
 remove_action('future_show','_future_post_hook');
 add_action('future_show','publish_future_post_now');
}

function publish_future_post_now($id) {
        wp_publish_post($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.

<?php
// 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 stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x