Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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