Make WordPress Core

Opened 4 years ago

Closed 3 months ago

#53418 closed defect (bug) (worksforme)

Post Status Transition missing Hook

Reported by: brettrans's profile brettrans Owned by:
Milestone: Priority: normal
Severity: blocker Version: 5.7.2
Component: Posts, Post Types Keywords: needs-patch needs-testing has-dev-note dev-feedback
Focuses: Cc:

Description

REF: https://codex.wordpress.org/Post_Status_Transitions

So I have been testing this and found there is an issue creating the post type new to pending:

<?php
// Sends a notification when a new Event type post is created from New to Pending.
function on_new_pending_post( $post ) {
        // A function to perform when a pending post is published.
        // Send an email:
}
add_action(  'new_to_pending',  'on_new_pending_post', 10, 1 );

This code above will run on both status changes from new to draft and new to pending

we have a form in which people submit new posts, these posts can be saved as a draft before being saved to pending status. We need to send an email when a status is changed from new to pending and draft to pending however we don't want an email to send from new to draft but using the above code hook will still send an email

We have attempted to use

<?php
function post_new_pending( $new_status, $old_status, $post ) {
    if ( $old_status == 'new'  &&  $new_status != 'pending' ) {
        // A function to perform actions when a post status changes from publish to any non-public status.
// send email
                }
    }
}
add_action( 'transition_post_status', 'post_new_pending', 10, 3 );

have tried with the $old_status set to empty or set to 'new' but this doesn't fire our code off.

we can simulate the status change from draft to pending correctly using

<?php
function post_draft_pending( $new_status, $old_status, $post ) {
    if ( $old_status == 'draft'  &&  $new_status != 'pending' ) {
        // A function to perform actions when a post status changes from publish to any non-public status.

                }
    }
}
add_action( 'transition_post_status', 'post_draft_pending', 10, 3 );

There doesn't seem to be a way to using new to pending even when using the hook new_to_pending it will also effect new to draft status changes.

Change History (1)

#1 @SirLouen
3 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

Reproduction Report

Description

❌ This report can't validate that the issue can be reproduced.

Environment

  • WordPress: 6.9-alpha-60093-src
  • PHP: 8.2.29
  • Server: nginx/1.29.1
  • Database: mysqli (Server: 8.4.6 / Client: mysqlnd 8.2.29)
  • Browser: Chrome 140.0.0.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty 2.9
  • MU Plugins: None activated
  • Plugins:
    • BBB Testing Dolly
    • Test Reports 1.2.0

Testing Instructions

  1. Using the code I provide in the supp artifacts
  2. I have create a very simple title/content form to create controlled posts. This is very important because the editors mess up with draft statuses and for this, we need a direct way to interact with wp_insert_post passing post_status (both draft and pending).
  3. 👌 Transitions are done as expected

Actual Results

  1. ❌ Error condition is not occurring

Additional Notes

  • I've been logging actions and transitions are being done timely and without concurrence. For this reason, I have to close this as worksforme. Feel free to check my code and my instructions, and reopen if more information for testing can be provided.

Supplemental Artifacts

Code:

add_action( 'transition_post_status', 'log_transitions', 10, 3 );
function log_transitions( $new_status, $old_status, $post ) {
	if ( ! ( ( 'new' === $old_status && 'draft' === $new_status ) ||
        ( 'draft' === $old_status && 'pending' === $new_status ) ||
        ( 'new' === $old_status && 'pending' === $new_status ) ) ) {
		return;
	}

	$message   = sprintf(
		'[%s] Post ID %d: %s -> %s',
		current_time( 'mysql' ),
		(int) $post->ID,
		$old_status,
		$new_status,
	);

	error_log( $message );
}

Logs:

[11-Sep-2025 23:43:29 UTC] [2025-09-11 23:43:29] Post ID 176 "fdasfa": new -> draft by admin
[11-Sep-2025 23:43:49 UTC] [2025-09-11 23:43:49] Post ID 177 "fdsafa": new -> pending by admin
[11-Sep-2025 23:44:04 UTC] [2025-09-11 23:44:04] Post ID 176 "fdasfa": draft -> pending by admin

Note: See TracTickets for help on using tickets.