WordPress.org

Make WordPress Core

Opened 4 months ago

#53418 new defect (bug)

Post Status Transition missing Hook

Reported by: brettrans Owned by:
Milestone: Awaiting Review 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 (0)

Note: See TracTickets for help on using tickets.