Custom Post Type Publish Action Hook

Written by on March 4, 2011 in WordPress Tutorials - 7 Comments

This is an extension of the post I wrote a while back that described how to perform an action whenever a post is published.

The action hook used in that post was for regular WordPress posts and looked like this:

1
2
3
4
5
6
7
8
// function to be executed when a post is published
function run_when_post_published()
{
    // your function code here
}
add_action('new_to_publish', 'run_when_post_published');
add_action('draft_to_publish', 'run_when_post_published');
add_action('pending_to_publish', 'run_when_post_published');

Well, what about custom post types? Unfortunately, the hooks above don’t fire when a custom post type is published. However, thankfully, we have another nearly identical hook that does. I discussed it briefly on Pippin’s Pages. At that time I was only aware of the hook that allowed an action to run whenever a custom post type was published, which also fired anytime a custom post type was edited. The hook I used then looked like this:

1
2
// replace {custom_post_type_name} with your post type name
add_action('publish_{custom_post_type_name}', 'function_to_perform');

However, thanks to an insistent customer of mine, I was recently forced to dig a little deep and see if I could find a hook that works for custom post types just like the regular publish post action hook. The most important thing being that the action be run only the first time the post is published, and not anytime that it’s edited as well.

So here’s what we have.

1
2
3
4
5
6
7
8
9
10
// function to be executed when a custom post type is published
function run_when_post_published()
{
    // your function code here
}
 
// replace {custom_post_type_name} with the name of your post type
add_action('new_to_publish_{custom_post_type_name}', 'run_when_post_published');		
add_action('draft_to_publish_{custom_post_type_name}', 'run_when_post_published');		
add_action('pending_to_publish_{custom_post_type_name}', 'run_when_post_published');

Using the combination of these three hooks, we can perform any action we wish anytime a custom post type is published.

To see it in a real world example, check out my Post Notify plugin from Code Canyon.net. For that plugin, I used action hooks just like these to send out, or schedule, emails to a list of subscribers announcing that a new post has been published.

Enjoy!

About

Pippin Williamson is an expert WordPress dev with 4+ years of experience. You may follow him on Twitter @pippinspages and @pippinsplugins and see his free WP plugins, themes, and tutorials he has to offer at Pippin's Pages.com and Pippin's Plugins.com

Post comment as twitter logo facebook logo
Sort: Newest | Oldest

Once I initially commented I clicked the -Notify me when new comments are added- checkbox and now each time a remark is added I get 4 emails with the same comment. Is there any manner you may take away me from that service? Thanks!

You will need to use publish_post, but you will also have to do a check inside of your function that determines if the post already exists or not because this hook runs on publish and update.

Hi Pippin,

What hook would we use when only updating a post type?

Thanks for the tutorial, I'm half-way there!

Hi,

Very good find. Btw, how we do that with category or taxonomy?

For example in category, there is hook called 'category_add_form', then in custom taxonomy, do you know how we call that hook?

I don't, sorry. It would most likely use "taxonomy" in its name, so something like "taxonomy_add_form".

thank you
i am waiting for wordpress core dev to add a wild_card in these kind of hooks such as
*_to_publish_{custom_post_type_name}

That would make things much easier. We would no longer need to list 6-7 hooks for one action.