最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

actions - Custom post type suppress transition from future to publish

programmeradmin0浏览0评论

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 );
        }
    }

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 );
        }
    }
Share Improve this question asked Sep 19, 2019 at 20:50 aswissaswiss 335 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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',
) );
发布评论

评论列表(0)

  1. 暂无评论