Opened 11 years ago
Closed 11 years ago
#31099 closed defect (bug) (fixed)
Hook action on publishing custom post type also firing on update
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 4.2 | Priority: | normal |
| Severity: | normal | Version: | 4.1 |
| Component: | Posts, Post Types | Keywords: | good-first-bug has-patch |
| Focuses: | docs | Cc: |
Description
I am using the following hook to add an action after my custom post type has been published.
add_action('publish_my-custom-post-type', 'my-function-on-publish');
As it turns out, however, it's actually firing every time the post is updated, not just when it's published. According to the handbook, however, it should only fire when status changes TO published.
PS: I'm seeing the same behavior with WordPress 3.9.X as well as 4.X
Attachments (2)
Change History (11)
#4
@
11 years ago
Well, first of all thanks for the quick confirmation as well as the provided alternative - top notch.
And yes, updating the docs (possibly including the above solution) seems like the way to go then.
#6
in reply to:
↑ 3
@
11 years ago
Replying to DrewAPicture:
One thing we could do here is adjust the docs for the
{$new_status}_{$post->post_type},{$old_status}_to_{$new_status}, andtransition_post_statushooks to reflect the expected behavior of the post status not changing when published posts are updated.
good-first-bug meet my-first-patch :)
I submit 31099Docs.patch which adds details for publish_{$post->type} suggesting transition_post_status which answers the scenario of the original defect.
Unfortunately, we can't change the behavior of the hook in this way due to back-compat concerns. Whether it seemingly makes sense or not, we have to consider that people now expect it to work the way it does for published posts.
If you're looking to only fire a callback when posts are published, I suggest using the transition_post_status hook instead:
One thing we could do here is adjust the docs for the
{$new_status}_{$post->post_type},{$old_status}_to_{$new_status}, andtransition_post_statushooks to reflect the expected behavior of the post status not changing when published posts are updated.