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!

This article was authored by:

Pippin Williamson is a WordPress developer based in Lawrence, KS. WordPress is his expertise: custom theme / plugin development, e-commerce solutions, content management, and everything else WordPress. His personal site can be found at PippinsPlugins.com. There he publishes frequent WordPress tutorials, tips and tricks. He also host a variety of his own free WordPress plugins. Join the site and receive member only benefits, including exclusive tutorial access and source file downloads.

Pippin Williamson has authored 42 posts.Visit Website

Showing 26 Comments

  • 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}

    REPLY
  • Pippin (Admin)

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

    REPLY
  • 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
  • Pippin (Admin)

    I don’t, sorry. It would most likely use “taxonomy” in its name, so something like “taxonomy_add_form”.

    REPLY
  • Hi Pippin,

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

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

    REPLY
  • Pippin (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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Looks like the discussed ‘{old_status}_to_{new_status}_{custom_post_type_name}’ action hook is no longer available in WordPress 3.5, and maybe even before that… ‘{old_status}_to_{new_status}’ should now be called for all post types but I have not tested this yet.

    Note that this implicates (at least in theory) that the hook ‘publish_to_publish’ is equal to ‘publish_post’, ‘publish_page’ and ‘publish_{custom_post_type_name}’ all at once.

    REPLY
  • @RavanH How can I publish to all post_types including posts, I would like to find something like a publish_anyposts or ‘publish_{all_custom_post_type_names}’ hook

    REPLY
  • It is really helpful for me :)

    REPLY
  • Really great and helpful post…

    Thanks…..

    REPLY
  • Could this be implemented to fire up a jQuery notification when a new post is published?

    Thanks for the hint!

    REPLY

Add Your Voice: