[wp-trac] [WordPress Trac] #31099: Hook action on publishing custom post type also firing on update

WordPress Trac noreply at wordpress.org
Thu Jan 22 19:28:57 UTC 2015


#31099: Hook action on publishing custom post type also firing on update
-------------------------------+------------------------------
 Reporter:  yosmc              |       Owner:
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Posts, Post Types  |     Version:  4.1
 Severity:  normal             |  Resolution:
 Keywords:  needs-patch        |     Focuses:  docs
-------------------------------+------------------------------
Changes (by DrewAPicture):

 * keywords:  has-patch => needs-patch
 * focuses:   => docs


Comment:

 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
 [https://developer.wordpress.org/reference/hooks/transition_post_status/
 transition_post_status] hook instead:

 {{{
 #!php
 function wpdocs_run_on_publish_only( $new_status, $old_status, $post ) {
                 if ( ( 'publish' == $new_status && 'publish' !==
 $old_status )
                         && 'my-custom-post-type' == $post->post_type
                 ) {
                         // do stuff
                 }
 }
 add_action( 'transition_post_status', 'wpdocs_run_on_publish_only' );
 }}}

 One thing we could do here is adjust the docs for the
 `$new_status_$post_type` hook to reflect the expected behavior of the post
 status not changing when published posts are updated.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/31099#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list