Question

I've scheduled a custom post type to a future datetime. Now I won't that it will be published automatically, instead it should switch the status to draft.

We use this to pulish the webinars, so we can show upcoming webinars on the archive page. But if the time is coming to "publish" the webinar page, we would like to set it to draft. After the webinar we can edit the draft and insert the recording. This is the reason why we would like to change post_status from future to draft and NOT to publish.

We've tried the code below, but without success. It seems that this hook will be fired before the post ist stored to the database. But to hook wp_insert_post or save_post_{$post->post_type} won't give us the information about the old status, so that our function will only run if status is changed from future to publish.

Does anoybody has an idea? :-)

add_action( 'future_to_publish', array( $this, 'transitionFutureToPublish' ) );

public function transitionFutureToPublish( $post ) {
        if ( ! empty( $post ) && $post->post_type == 'webinar' ) {
            $post['post_status'] = 'draft';

            wp_update_post( $post );
        }
    }
Was it helpful?

Solution

The $post you're working with is a WP_Post Object and you must interact with it as so.

$post->post_status = 'draft';

It's usually not a good practice to modify the $post object directly so it's something you may want to avoid. A better way may be to simply pass an array of values to wp_update_post() function like so:

wp_update_post( array(
    'ID'            => $post->ID,
    'post_status'   => 'draft',
) );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top