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 action publishpressfuture_expire 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 (defined('PUBLISHPRESS_FUTURE_LOADED')) {
    /**
     * 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'));
    do_action(
        'publishpressfuture_schedule_expiration',
        $postId,
        $timestamp,
        $options
    );
}

To remove the expiration progamatically, try this code:

if (defined('PUBLISHPRESS_FUTURE_LOADED')) {
    $postId = 258;
    do_action('publishpressfuture_unschedule_expiration', $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