check_and_publish_future_post

The timeline below displays how wordpress function check_and_publish_future_post has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Publishes future post and make sure post ID has future post status.
 *
 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
 * from publishing drafts, etc.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post Post ID or post object.
 */
function check_and_publish_future_post($post)
{
    $post = get_post($post);
    if (!$post) {
        return;
    }
    if ('future' !== $post->post_status) {
        return;
    }
    $time = strtotime($post->post_date_gmt . ' GMT');
    // Uh oh, someone jumped the gun!
    if ($time > time()) {
        wp_clear_scheduled_hook('publish_future_post', array($post->ID));
        // Clear anything else in the system.
        wp_schedule_single_event($time, 'publish_future_post', array($post->ID));
        return;
    }
    // wp_publish_post() returns no meaningful value.
    wp_publish_post($post->ID);
}

WordPress Version: 5.6

/**
 * Publish future post and make sure post ID has future post status.
 *
 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
 * from publishing drafts, etc.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post_id Post ID or post object.
 */
function check_and_publish_future_post($post_id)
{
    $post = get_post($post_id);
    if (!$post) {
        return;
    }
    if ('future' !== $post->post_status) {
        return;
    }
    $time = strtotime($post->post_date_gmt . ' GMT');
    // Uh oh, someone jumped the gun!
    if ($time > time()) {
        wp_clear_scheduled_hook('publish_future_post', array($post_id));
        // Clear anything else in the system.
        wp_schedule_single_event($time, 'publish_future_post', array($post_id));
        return;
    }
    // wp_publish_post() returns no meaningful value.
    wp_publish_post($post_id);
}

WordPress Version: 5.5

/**
 * Publish future post and make sure post ID has future post status.
 *
 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
 * from publishing drafts, etc.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post_id Post ID or post object.
 */
function check_and_publish_future_post($post_id)
{
    $post = get_post($post_id);
    if (empty($post)) {
        return;
    }
    if ('future' !== $post->post_status) {
        return;
    }
    $time = strtotime($post->post_date_gmt . ' GMT');
    // Uh oh, someone jumped the gun!
    if ($time > time()) {
        wp_clear_scheduled_hook('publish_future_post', array($post_id));
        // Clear anything else in the system.
        wp_schedule_single_event($time, 'publish_future_post', array($post_id));
        return;
    }
    // wp_publish_post() returns no meaningful value.
    wp_publish_post($post_id);
}

WordPress Version: 5.4

/**
 * Publish future post and make sure post ID has future post status.
 *
 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
 * from publishing drafts, etc.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post_id Post ID or post object.
 */
function check_and_publish_future_post($post_id)
{
    $post = get_post($post_id);
    if (empty($post)) {
        return;
    }
    if ('future' != $post->post_status) {
        return;
    }
    $time = strtotime($post->post_date_gmt . ' GMT');
    // Uh oh, someone jumped the gun!
    if ($time > time()) {
        wp_clear_scheduled_hook('publish_future_post', array($post_id));
        // Clear anything else in the system.
        wp_schedule_single_event($time, 'publish_future_post', array($post_id));
        return;
    }
    // wp_publish_post() returns no meaningful value.
    wp_publish_post($post_id);
}

WordPress Version: 4.6

/**
 * Publish future post and make sure post ID has future post status.
 *
 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
 * from publishing drafts, etc.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post_id Post ID or post object.
 */
function check_and_publish_future_post($post_id)
{
    $post = get_post($post_id);
    if (empty($post)) {
        return;
    }
    if ('future' != $post->post_status) {
        return;
    }
    $time = strtotime($post->post_date_gmt . ' GMT');
    // Uh oh, someone jumped the gun!
    if ($time > time()) {
        wp_clear_scheduled_hook('publish_future_post', array($post_id));
        // clear anything else in the system
        wp_schedule_single_event($time, 'publish_future_post', array($post_id));
        return;
    }
    // wp_publish_post() returns no meaningful value.
    wp_publish_post($post_id);
}

WordPress Version: 4.4

/**
 * Publish future post and make sure post ID has future post status.
 *
 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
 * from publishing drafts, etc.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post_id Post ID or post object.
 */
function check_and_publish_future_post($post_id)
{
    $post = get_post($post_id);
    if (empty($post)) {
        return;
    }
    if ('future' != $post->post_status) {
        return;
    }
    $time = strtotime($post->post_date_gmt . ' GMT');
    // Uh oh, someone jumped the gun!
    if ($time > time()) {
        wp_clear_scheduled_hook('publish_future_post', array($post_id));
        // clear anything else in the system
        wp_schedule_single_event($time, 'publish_future_post', array($post_id));
        return;
    }
    // wp_publish_post(_ returns no meaningful value.
    wp_publish_post($post_id);
}

WordPress Version: 4.3

/**
 * Publish future post and make sure post ID has future post status.
 *
 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
 * from publishing drafts, etc.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post_id Post ID or post object.
 */
function check_and_publish_future_post($post_id)
{
    $post = get_post($post_id);
    if (empty($post)) {
        return;
    }
    if ('future' != $post->post_status) {
        return;
    }
    $time = strtotime($post->post_date_gmt . ' GMT');
    // Uh oh, someone jumped the gun!
    if ($time > time()) {
        wp_clear_scheduled_hook('publish_future_post', array($post_id));
        // clear anything else in the system
        wp_schedule_single_event($time, 'publish_future_post', array($post_id));
        return;
    }
    // wp_publish_post(_ returns no meaninful value
    wp_publish_post($post_id);
}

WordPress Version: 4.0

/**
 * Publish future post and make sure post ID has future post status.
 *
 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
 * from publishing drafts, etc.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post_id Post ID or post object.
 * @return null Nothing is returned. Which can mean that no action is required
 *              or post was published.
 */
function check_and_publish_future_post($post_id)
{
    $post = get_post($post_id);
    if (empty($post)) {
        return;
    }
    if ('future' != $post->post_status) {
        return;
    }
    $time = strtotime($post->post_date_gmt . ' GMT');
    // Uh oh, someone jumped the gun!
    if ($time > time()) {
        wp_clear_scheduled_hook('publish_future_post', array($post_id));
        // clear anything else in the system
        wp_schedule_single_event($time, 'publish_future_post', array($post_id));
        return;
    }
    return wp_publish_post($post_id);
}

WordPress Version: 3.9

/**
 * Publish future post and make sure post ID has future post status.
 *
 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
 * from publishing drafts, etc.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post_id Post ID or post object.
 * @return null Nothing is returned. Which can mean that no action is required or post was published.
 */
function check_and_publish_future_post($post_id)
{
    $post = get_post($post_id);
    if (empty($post)) {
        return;
    }
    if ('future' != $post->post_status) {
        return;
    }
    $time = strtotime($post->post_date_gmt . ' GMT');
    if ($time > time()) {
        // Uh oh, someone jumped the gun!
        wp_clear_scheduled_hook('publish_future_post', array($post_id));
        // clear anything else in the system
        wp_schedule_single_event($time, 'publish_future_post', array($post_id));
        return;
    }
    return wp_publish_post($post_id);
}

WordPress Version: 3.7

/**
 * Publish future post and make sure post ID has future post status.
 *
 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
 * from publishing drafts, etc.
 *
 * @since 2.5.0
 *
 * @param int $post_id Post ID.
 * @return null Nothing is returned. Which can mean that no action is required or post was published.
 */
function check_and_publish_future_post($post_id)
{
    $post = get_post($post_id);
    if (empty($post)) {
        return;
    }
    if ('future' != $post->post_status) {
        return;
    }
    $time = strtotime($post->post_date_gmt . ' GMT');
    if ($time > time()) {
        // Uh oh, someone jumped the gun!
        wp_clear_scheduled_hook('publish_future_post', array($post_id));
        // clear anything else in the system
        wp_schedule_single_event($time, 'publish_future_post', array($post_id));
        return;
    }
    return wp_publish_post($post_id);
}