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 = StackExchange\WordPress;
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 );
register_post_type
callsregistered_post_type
. Maybe you can hook your 'publish hook' when the custom post type gets registered, using the the arguments ofregistered_post_type
to get the name of custom post typeregistered_post_type
is called continuously, for all types of post (including WordPress private ones, likerevision
,attachment
,nav_menu_item
, and so on), so I can't use it for this purpose.