In my plugin, I have a custom post type with a specific behaviour.

The administrator after to publish it, he should not be able to set the post as draft again.

How can I do this ?

2 Answers
2

I would recommend the transition_post_status hook, which allows you to hook into when a post is transitioning (or anytime a post is saved). In the example below, we check if the post is apart of your custom post type. Then we see if the old status was published and if the new status is not published, we throw an error.

<?php 
add_action( 'transition_post_status', 'tps_no_draft_after_publish', 10, 3 );
function tps_no_draft_after_publish( $new_status, $old_status, $post ) {
    if ( ( 'publish' !== $new_status && 'publish' === $old_status ) && 'my-custom-post-type' === $post->post_type
    ) {
        wp_die('Posts that have been published cannot be set as draft.');
    }
}

Additional reading:

  • https://developer.wordpress.org/reference/hooks/transition_post_status/
  • https://developer.wordpress.org/reference/hooks/save_post/

Leave a Reply

Your email address will not be published. Required fields are marked *