Technical Details for PublishPress Future

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.


Adding an expiry date to posts

On every post save, the PublishPress Future 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.

About the PublishPress Future cron jobs

The cron task itself only has a name of the hook: postExpiratorExpire, and the post ID.

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. 


What happens when the cron job runs

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

Next, the cron task will reads the post meta for the specific post, looking for the other settings, such as the expire type.

Finally, the cron task handles the expiration according to the selected for the post.


How to schedule expirations programmatically

For scheduling post expiration 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);
}

To remove the expiration progamatically, try this code:

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

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