Title: Setting expiration options programmatically
Last modified: August 21, 2016

---

# Setting expiration options programmatically

 *  Resolved [tiszenkel](https://wordpress.org/support/users/tiszenkel/)
 * (@tiszenkel)
 * [13 years ago](https://wordpress.org/support/topic/setting-expiration-options-programmatically/)
 * So post expirator is working great for me, but I’ve got one issue. I’m creating
   a certain category of posts programmatically and want to set them to expire after
   five days. To do that, I’m using this code when I create the post:
 *     ```
       $current_time = date(U);
       $expire_time = $current_time + 432000; // 5 days is 432000 seconds
       update_post_meta($post_id, '_expiration-date', (string)$expire_time);
       update_post_meta($post_id, '_expiration-date-options', 'a:2:{s:10:"expireType";s:6:"delete";s:2:"id";i:'.$post_id.';}');
       update_post_meta($post_id, '_expiration-date-status', 'saved');
       ```
   
 * If you look at the resulting post in the dashboard, you’d see that post expirator
   is turned on (it’s turned off by default) and the post expiration date is just
   as intended. But the post is set to expire to a draft, not to the trash, as it’s
   supposed to. And ultimately, it never does expire, to a draft OR to the trash.(
   I tested this by setting the expiration date to a few minutes in the future instead
   of five days.)
 * Any idea what I’m doing wrong here?
 * [http://wordpress.org/extend/plugins/post-expirator/](http://wordpress.org/extend/plugins/post-expirator/)

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Aaron Axelsen](https://wordpress.org/support/users/axelseaa/)
 * (@axelseaa)
 * [13 years ago](https://wordpress.org/support/topic/setting-expiration-options-programmatically/#post-3826334)
 * That will never work. With version 2.0, the logic changed on how the plugin works.
   It now schedules a unique cron event for each post as needed to save on system
   resources. Just setting the meta will no longer do the trick
 *  Thread Starter [tiszenkel](https://wordpress.org/support/users/tiszenkel/)
 * (@tiszenkel)
 * [13 years ago](https://wordpress.org/support/topic/setting-expiration-options-programmatically/#post-3826363)
 * Bummer! Well, I have a once-a-day delete script ready to go, so that’s Plan B—
   just seemed like it would be redundant when I had Post Expirator installed.
 *  [Aaron Axelsen](https://wordpress.org/support/users/axelseaa/)
 * (@axelseaa)
 * [13 years ago](https://wordpress.org/support/topic/setting-expiration-options-programmatically/#post-3826365)
 * If you look through the plugin code, I did break out the scheduling bit into 
   a function (_scheduleExpiratorEvent) that you could in theory call yourself with
   the correct arguments.
 *  Thread Starter [tiszenkel](https://wordpress.org/support/users/tiszenkel/)
 * (@tiszenkel)
 * [13 years ago](https://wordpress.org/support/topic/setting-expiration-options-programmatically/#post-3826471)
 * Managed to get it working! Unfortunately, just calling the function didn’t do
   the trick, because I’m using a script that runs before plugins are loaded. So
   I had to use the code independent of the function:
 *     ```
       if (wp_next_scheduled('postExpiratorExpire',array($post_id)) !== false) {
       wp_clear_scheduled_hook('postExpiratorExpire',array($post_id)); //Remove any existing hooks
       }
   
       wp_schedule_single_event($expire_time,'postExpiratorExpire',array($post_id)); 
   
       // Update Post Meta
       update_post_meta($post_id, '_expiration-date', $expire_time);
       update_post_meta($post_id, '_expiration-date-options', $opts);
       update_post_meta($post_id, '_expiration-date-status','saved');
       ```
   
 * Thanks, Aaron! You’ve got a terrific plugin that’s going to provide some key 
   functionality on a site I’m launching this weekend. This is just the last piece.
 *  [matt_teamworks](https://wordpress.org/support/users/matt_teamworks/)
 * (@matt_teamworks)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/setting-expiration-options-programmatically/#post-3826574)
 * Hi, I’m trying to achieve this exact scenario but I’m also struggling to get 
   the posts to update via wp_schedule_single_event.
 * A little background… I’ve created a custom post status called ‘archive’. (I want
   to be able to archive posts but not have them mixed in with genuine draft posts).
   In order to get this as an option in the post expirator plugin I had to manually
   add it to the ‘_postExpiratorExpireType’ and ‘postExpiratorExpire’ functions 
   in post-expirator.php. (This is not ideal as I wanted to avoid editing the plugin
   file directly – as a future update it would be great if there was a hook to allow
   adding custom post statuses)
 * My custom post status is setup as follows:
 *     ```
       function jc_custom_post_status(){
            register_post_status( 'archive', array(
                 'label'                     => _x( 'Archive', 'post' ),
                 'public'                    => false,
                 'show_in_admin_all_list'    => true,
                 'show_in_admin_status_list' => true,
                 'label_count'               => _n_noop( 'Archive <span class="count">(%s)</span>', 'Archive <span class="count">(%s)</span>' )
            ) );
       }
       add_action( 'init', 'jc_custom_post_status' );
       ```
   
 * I’m creating posts programmatically and setting them to archive as necessary 
   using the following function:
 *     ```
       // archive post
       function archivePost($post_id, $archiveDate) {
       	$expire_time = strtotime(formatTime($archiveDate));
       	$opts = 'a:2:{s:10:"expireType";s:7:"archive";s:2:"id";i:'.$post_id.';}';
   
       	if (wp_next_scheduled('postExpiratorExpire',array($post_id)) !== false) {
       		wp_clear_scheduled_hook('postExpiratorExpire',array($post_id)); //Remove any existing hooks
       	}
   
       	wp_schedule_single_event($expire_time,'postExpiratorExpire',array($post_id)); 
   
       	// Update Post Meta
       	update_post_meta($post_id, '_expiration-date', $expire_time);
       	update_post_meta($post_id, '_expiration-date-options', $opts);
       	update_post_meta($post_id, '_expiration-date-status','saved');
       }
       ```
   
 * When I edit the post in the admin I see that the expire posts option is ticked
   and the date is correct, but the post never expires. If I hit ‘update’ then the
   post expires as expected.
 * It also works fine if I create the post via the wordpress interface instead of
   programmatically.
 * I’ve tried comparing the values stored in wp_postmeta for a post created via 
   the admin and one created programmatically and they appear the same.
 * Any suggestions greatly appreciated.
 *  [matt_teamworks](https://wordpress.org/support/users/matt_teamworks/)
 * (@matt_teamworks)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/setting-expiration-options-programmatically/#post-3826575)
 * Fixed it by changing the opts line in my archivePost function.
 * from:
 * `$opts = 'a:2:{s:10:"expireType";s:7:"archive";s:2:"id";i:'.$post_id.';}';`
 * to:
 *     ```
       $opts = array(
       	'expireType' => 'archive',
       	'id' => $post_id
       );
       ```
   
 * Seems it needs to be an array (not a string as in the first post of this thread).
 * It turns out that I’m also able to call the function ‘_scheduleExpiratorEvent’
   directly which makes things a bit easier.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Setting expiration options programmatically’ is closed to new replies.

 * ![](https://ps.w.org/post-expirator/assets/icon-256x256.png?rev=3118683)
 * [Schedule Post Changes With PublishPress Future: Unpublish, Delete, Change Status, Trash, Change Categories](https://wordpress.org/plugins/post-expirator/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/post-expirator/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/post-expirator/)
 * [Active Topics](https://wordpress.org/support/plugin/post-expirator/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/post-expirator/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/post-expirator/reviews/)

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 6 replies
 * 3 participants
 * Last reply from: [matt_teamworks](https://wordpress.org/support/users/matt_teamworks/)
 * Last activity: [12 years, 4 months ago](https://wordpress.org/support/topic/setting-expiration-options-programmatically/#post-3826575)
 * Status: resolved