Technical Details for Post Expirator

In another documentation article, we explained that the scheduling is handled by a cron job and the _postmeta table.

This post will explain more about that process and how it works.

On every post save, the Post Expirator plugin will check if the expiration feature is enabled in the post.

  • If the feature is enabled, the plugin stores the data set in the post expire metabox in the _postmeta table, and schedules a cron task. 
  • If the feature is not enabled, it will remove any cron task for it.

The cron task itself only has a name of the hook: postExpiratorExpire, and the post ID in the args.
By default, the cron task is handled by WordPress, so it can be enabled (undefined or DISABLE_WP_CRON = true), or disabled (DISABLE_WP_CRON = false).

  • If WordPress is handling the cron, WordPress will trigger the cron job based on visits to site. That is not very precise for sites with low traffic.
  • If WordPress is not handling the cron, additional settings should be done to make sure there is a cronjob in the server running the cron tasks periodically.

The cron task is scheduled with the selected data and time for the expiration. 

When the cron task runs, it will call the function postExpiratorExpire which will receive the post ID found in the cron task argument.

Then it reads the post meta for the specific post, looking for the other settings, such as the expire type. Then it handles the expiration according to the selected for the post.

For scheduling post expirations programmatically, try this code:

if (function_exists('postExpirator_init')) {
     * Expiration types:
     *  - draft
     *  - delete
     *  - trash
     *  - private
     *  - stick
     *  - unstick
     *  - category
     *  - category-add
     *  - category-remove
     *  For "category", add additional attributes:
     *   'category' => $categoryId,
     *   'categoryTaxonomy' => $taxonomyName
    $options   = [
        'expireType' => 'delete',
        'id'         => $postId,
    $postId    = 258;
    $timeStamp = date('U', strtotime('2021-06-25 10:00:00'));

    _scheduleExpiratorEvent($postId, $timeStamp, $options);

For unscheduling posts, try this code:

if (function_exists('postExpirator_init')) {
    $postId = 258;

The actual expiration date for the post is stored both in the _expiration-date row of the _postmeta table and in the cron job.

Professional publishing plugins for WordPress! Get PublishPress