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_name
s (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).
Any hint?
What about using
save_post
orwp_insert_post
hook, both haveas 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 theis_custom_post_type()
function by @toscho.So here we go:
Shortly, only newly created custom post type posts are addressed, while there is no need to know the custom post types name.
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 topublish_$post_type
if the post type isn’t a WordPress default. Below is a sample plugin that will do that.Or, we can use the
transition_post_status
hook