With what hook can I address all posts from all custom post types when they are published?

I want that when every Custom Post Type is published, a hook is called (where I send an email notification to the administrator). I know I can use:

add_action('publish_{custom_post_type_name}', 'function_to_call');

but I don’t know the custom_post_type_names (every site has different Custom Post Types registered). In addition, I can’t take for granted that the Custom Post Types will always remain the same (because at any time other plugins or themes may register/deregister Custom Post Types).

Read More

Any hint?

Related posts

2 comments

  1. What about using save_post or wp_insert_post hook, both have

    @param     int               $post_ID     Post ID.
    @param     WP_Post     $post          Post object.
    @param     bool            $update     Whether this is an existing post being updated or not.

    as parameters. It is pretty self-explaining that the $update can be used to determine, if it is a new post or not. Additionally you will need to check for is it a custom post type, I use the is_custom_post_type() function by @toscho.

    So here we go:

    function wpse135423_function_to_call( $post_id, $post, $update ) {
        if ( wp_is_post_autosave( $post_id ) ) {
            return 'do nothing, because it is an autosave';
        }
        if ( wp_is_post_revision( $post_id ) ) {
            return 'do nothing, because it is a revision';
        }
        // if it is an update 
        if ( $update == TRUE ) {
            return 'do nothing, because it is an update';
        }
        // if it is a custom post type 
        if ( is_custom_post_type() == TRUE ) {
            // code to do whatever you want
        }
    }
    // Do NOT use both hooks!
    add_action( 'save_post', 'wpse135423_function_to_call', 10, 3 );
    add_action( 'wp_insert_post', 'wpse135423_function_to_call', 10, 3 );
    

    Shortly, only newly created custom post type posts are addressed, while there is no need to know the custom post types name.

  2. There isn’t one hook that will fire when all custom post types are published. But we can accomplish the same thing using a combination of hooks and conditionals. There are at least two ways to do it, in addition to the other answer. The first is to hook into register_post_type and conditionally add a hook to publish_$post_type if the post type isn’t a WordPress default. Below is a sample plugin that will do that.

    namespace = StackExchangeWordPress;
    class plugin {
      protected $defaultPostTypes = [
        'post', 'page', 'attachment', 'revision',
        'nav_menu_item', 'custom_css', 'customize_changeset',
      ];
      public function plugins_loaded() {
        add_action( 'registered_post_type', [ $this, 'registered_post_type' ], 10, 2 );
      }
      public function registered_post_type( string $post_type, WP_Post_Type $post_type_object ) {
        if( in_array( $post_type, $this->defaultPostTypes ) ) return;
        add_action( "publish_$post_type", [ $this, 'publish_post_type' ], 10, 3 );
      }
      public function publish_post_type( int $post_ID, WP_Post $post, bool $update ) {
        //* This method will execute whenever a custom post type is published.
      }
    }
    add_action( 'plugins_loaded', [ new plugin(), 'plugins_loaded' ] );
    

    Or, we can use the transition_post_status hook

    function wpse_135423_transition_post_status( $new_status, $old_status, $post ) {
      //* Make sure the new status is published
      if( 'publish' !== $new_status ) return;
    
      //* And that the post_type isn't one of the WordPress defaults
      $default = array(
        'post', 
        'page', 
        'attachment', 
        'revision',
        'nav_menu_item', 
        'custom_css', 
        'customize_changeset',
      );
      if( in_array( $post->post_type, $default ) ) return;
    
      //* A custom post type just got published.
    }
    add_action( 'transition_post_status', 'wpse_135423_transition_post_status', 10, 3 );
    

Comments are closed.