Custom Post Type Publish Action Hook
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!


Showing 21 Comments
arena
thank you
REPLY 809 days agoi am waiting for wordpress core dev to add a wild_card in these kind of hooks such as
*_to_publish_{custom_post_type_name}
Pippin (Admin)
That would make things much easier. We would no longer need to list 6-7 hooks for one action.
REPLY 809 days agoHudarsono
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?
REPLY 730 days agoPippin (Admin)
I don’t, sorry. It would most likely use “taxonomy” in its name, so something like “taxonomy_add_form”.
REPLY 730 days agoEthan
Hi Pippin,
What hook would we use when only updating a post type?
Thanks for the tutorial, I’m half-way there!
REPLY 725 days agoPippin (Admin)
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.
REPLY 724 days agoHenrik Bernström
Hi Pippin!
I have learnt that when using hooks the function you specify is run _before_ the actual save of the custom post type (together with additional fields and stuff).
Do you know of any way to have this function be executed _after_ the actual post type is saved to db? Should I insert / update the post type into db myself? Feels really awkward and not correct at all.
Thanks in advance!
REPLY 579 days agoHenrik Bernström
Hi Pippin!
I have learnt that when using hooks the function you specify is run _before_ the actual save of the custom post type (together with additional fields and stuff).
Do you know of any way to have this function be executed _after_ the actual post type is saved to db? Should I insert / update the post type into db myself? Feels really awkward and not correct at all.
Thanks in advance!
REPLY 579 days agoHenrik Bernström
Hi Pippin!
I have learnt that when using hooks the function you specify is run _before_ the actual save of the custom post type (together with additional fields and stuff).
Do you know of any way to have this function be executed _after_ the actual post type is saved to db? Should I insert / update the post type into db myself? Feels really awkward and not correct at all.
Thanks in advance!
REPLY 579 days agoHenrik Bernström
Hi Pippin!
I have learnt that when using hooks the function you specify is run _before_ the actual save of the custom post type (together with additional fields and stuff).
Do you know of any way to have this function be executed _after_ the actual post type is saved to db? Should I insert / update the post type into db myself? Feels really awkward and not correct at all.
Thanks in advance!
REPLY 579 days agoHenrik Bernström
Hi Pippin!
I have learnt that when using hooks the function you specify is run _before_ the actual save of the custom post type (together with additional fields and stuff).
Do you know of any way to have this function be executed _after_ the actual post type is saved to db? Should I insert / update the post type into db myself? Feels really awkward and not correct at all.
Thanks in advance!
REPLY 579 days agoHenrik Bernström
Hi Pippin!
I have learnt that when using hooks the function you specify is run _before_ the actual save of the custom post type (together with additional fields and stuff).
Do you know of any way to have this function be executed _after_ the actual post type is saved to db? Should I insert / update the post type into db myself? Feels really awkward and not correct at all.
Thanks in advance!
REPLY 579 days agoHenrik Bernström
Hi Pippin!
I have learnt that when using hooks the function you specify is run _before_ the actual save of the custom post type (together with additional fields and stuff).
Do you know of any way to have this function be executed _after_ the actual post type is saved to db? Should I insert / update the post type into db myself? Feels really awkward and not correct at all.
Thanks in advance!
REPLY 579 days agoHenrik Bernström
Hi Pippin!
I have learnt that when using hooks the function you specify is run _before_ the actual save of the custom post type (together with additional fields and stuff).
Do you know of any way to have this function be executed _after_ the actual post type is saved to db? Should I insert / update the post type into db myself? Feels really awkward and not correct at all.
Thanks in advance!
REPLY 579 days agoHenrik Bernström
Hi Pippin!
I have learnt that when using hooks the function you specify is run _before_ the actual save of the custom post type (together with additional fields and stuff).
Do you know of any way to have this function be executed _after_ the actual post type is saved to db? Should I insert / update the post type into db myself? Feels really awkward and not correct at all.
Thanks in advance!
REPLY 579 days agoHenrik Bernström
Hi Pippin!
I have learnt that when using hooks the function you specify is run _before_ the actual save of the custom post type (together with additional fields and stuff).
Do you know of any way to have this function be executed _after_ the actual post type is saved to db? Should I insert / update the post type into db myself? Feels really awkward and not correct at all.
Thanks in advance!
REPLY 579 days agoHenrik Bernström
Hi Pippin!
I have learnt that when using hooks the function you specify is run _before_ the actual save of the custom post type (together with additional fields and stuff).
Do you know of any way to have this function be executed _after_ the actual post type is saved to db? Should I insert / update the post type into db myself? Feels really awkward and not correct at all.
Thanks in advance!
REPLY 579 days agoHenrik Bernström
Hi Pippin!
I have learnt that when using hooks the function you specify is run _before_ the actual save of the custom post type (together with additional fields and stuff).
Do you know of any way to have this function be executed _after_ the actual post type is saved to db? Should I insert / update the post type into db myself? Feels really awkward and not correct at all.
Thanks in advance!
REPLY 579 days agoHenrik Bernström
Hi Pippin!
I have learnt that when using hooks the function you specify is run _before_ the actual save of the custom post type (together with additional fields and stuff).
Do you know of any way to have this function be executed _after_ the actual post type is saved to db? Should I insert / update the post type into db myself? Feels really awkward and not correct at all.
Thanks in advance!
REPLY 579 days agoLucia
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!
REPLY 554 days agoRune
Is there any update on these three action hooks, i just tested them all and none of them worked for me, only the basic publish_post_type gave me any result.
REPLY 13 days ago