Opened 4 years ago
Closed 3 months ago
#53418 closed defect (bug) (worksforme)
Post Status Transition missing Hook
| Reported by: |
|
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.
Reproduction Report
Description
❌ This report can't validate that the issue can be reproduced.
Environment
Testing Instructions
wp_insert_postpassingpost_status(both draft and pending).Actual Results
Additional Notes
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: