How to change post status in hook?

I have similar problem as described in How to trap “Publish” button to check for meta box validation?
Answer there is to hook into save_post and change post type. How can I do it?
I try to use wp_transition_post_status but it doesn’t work for me…

function myHook( $post_ID, $post )
{

    wp_transition_post_status('pending', $post->post_status, $post );

}

add_action( 'save_post', 'myHook', 10, 2 );

Edit: I have clear wordpress installation without any plugins, additional code and similar

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You get the full post object as a second parameter on save_post. Use it to change the status just like the following code.

add_action( 'save_post', 'wpse_78351_status', 10, 2 );

function wpse_78351_status( $post_ID, $post )
{
    remove_filter( current_filter(), __FUNCTION__ );

    if ( 'trash' !== $post->post_status ) //adjust the condition
    {
        $post->post_status = 'draft'; // use any post status
        wp_update_post( $post );
    }
}

See this answer for a list of post statuses.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x