Support » Developing with WordPress » Post Status Transitions Hooks dont fire in quick edit

  • Resolved leorospo


    Hi there!
    I’m building a community site for a client and i’m trying to save the date when a CPT (‘racconti’) changes status from draft to pending in a post meta. I also want it to be deleted when the ‘racconti’ changes from any status to draft.
    Here is the code i wrote.

    			//Draft to pending adds value
    function draft_to_pending_date_time_updater() {
    	$post_id = get_the_ID();
    	$meta_value = current_time( 'Y-m-d H:i:s' );
    	update_post_meta( $post_id, 'draft_to_pending_date_time', $meta_value );
    add_action(  'draft_to_pending',  'draft_to_pending_date_time_updater' );
    			//Any state to draft removes value
    function racconti_to_draft_date_time_deleter( $ID, $post ) {
    	$post_id = get_the_ID();
    	update_post_meta( $post_id, 'draft_to_pending_date_time', '' );
    add_action(  'draft_racconti',  'racconti_to_draft_date_time_deleter');

    The functions are working properly both from the classic editor (not tested with Gutenberg) and in the front end editor i wrote but are not working if the post status is updated via quick edit.

    My guess is that those hooks used are not fired when a quick edit occurs.
    Do you have any other explaination?

    I also tried replacing
    $post_id = get_the_ID();


    global $post;
    $post_id = $post->ID;

    I really hope to find an answer here
    if it is as i think should I suggest fixing this in the codex for a consistency problem?

    have a nice day


Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz


    Your methods of getting the post ID are invalid in this scenario because the normal “loop” which sets the global $post variable to the current post does not occur with quick edit. get_the_ID() relies upon the related global in $GLOBALS[‘post’], so that’s not an option either. In any case, there’s no need to get the current post that way because the action hook passes the current post object to your callback.

    The hooks you are using are indeed firing, but your code is ineffective because it relies on unreliable (for the quick edit scenario) global $post data. Here’s my modifications of your code to get the ID from the passed post object:

    function draft_to_pending_date_time_updater( $post ) {
    	$meta_value = current_time( 'Y-m-d H:i:s' );
    	update_post_meta( $post->ID, 'draft_to_pending_date_time', $meta_value );
    add_action('draft_to_pending', 'draft_to_pending_date_time_updater');
    function racconti_to_draft_date_time_deleter( $ID, $post ) {
    	update_post_meta( $ID, 'draft_to_pending_date_time', '' );
    add_action('draft_racconti', 'racconti_to_draft_date_time_deleter', 10, 2 );

    Also note the added parameters to the second add_action() call. When your callback collects more than one passed value ( such as $ID, $post ), you must supply the number passed in the add_action() call, or a PHP warning is thrown.

    For the ‘draft_racconti’ action $ID == $post->ID, so core code’s passing of the ID alone is a little redundant. Be that as it may, that is what it does so you may as well make use of it 🙂

    Edited to add:
    In draft_to_pending_date_time_updater(), this will be applied to any post going from draft to pending. While adding post meta to other post types does no harm, it’s inefficient if not needed. Consider first checking if 'racconti' == $post->post_type before updating post meta if you only intend to target that post type alone.

    • This reply was modified 3 months, 1 week ago by  bcworkz. Reason: removed superfluous semicolon after function(){}

    Thanks a lot for the explainations
    And thanks expecially for being so mindful noticing my carelessnes in the draft to pending function.


Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.