How can I specify the post status of an untrashed post?

By default WordPress assigns the draft status to a post that has been untrashed. I would like to assign the pending status to posts that are untrashed.

This seems to be possible with wp_untrash_post_status, but I can’t seem to find the proper way to use it. I have this in my code :

add_action( 'untrash_post', 'my_function' );
function my_function( $post_id ) {
  apply_filters( 'wp_untrash_post_status', 'pending', $post_id, 'pending' );
}

What am I doing wrong ?

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

I think for your purpose wp_untrash_post_status filter will be enough. Will work with single and multiple posts restore.

Filters the status that a post gets assigned when it is restored from the trash (untrashed).

add_filter( 'wp_untrash_post_status', 'change_untrash_post_status');

function change_untrash_post_status($status){
    return 'pending';
}

P.S. apply_filtershook is used to call callback functions created by us, like a snippet above, so apply_filters will fire a callback function which we added with add_filter (As I understood wp logic right)


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