How to do conditional publishing?


I am trying to write a plugin that will stop a user from publishing (or updating) a post if a given condition is not met, for example, if the title of the post is already being used in a different post (I am using a custom post type, if that makes a difference).
I wrote a function that gets called through the ‘transition_post_status’ hook to attempt to catch the post before it gets published or updated in the database, so that I can stop that from happening.
When I test this out with a title I know I’ve already used I see the error message when the wp_die() function happens, so I know this function is getting called. However, the post is being created anyways. Is there something I’m missing? Is this even the best way of checking for conditions before letting a post be saved in the database?

This is what I have in the constructor:

add_action( 'transition_post_status', array( $this, 'post_publish_control' ), 10, 3 );

Later in the class:

public function post_publish_control( $new_status, $old_status, $post ) {

   // Make sure this function runs on correct post type
   if( $post->post_type === 'my_custom_post_type' ) {

      // On first-time publish or post update
      if( $new_status === 'publish' ) {

         // Try to catch an already existing title before post is saved
         if( does_post_title_exist( $post->post_title ) ) {
            wp_die( 'Title is already being used, try a different one.' );
         // Continue if conditions are met
         else {

I am a bit more used to object-oriented programming, so I’m trying to write this plugin as its own class.

David Valenzuela 2 years 2021-07-05T20:44:48-05:00 0 Answers 0 views 0

Leave an answer