Undelete post (untrash)?

Which function should be used to move post from trash to published pages?
i.e. is there wp_undelete_post or something like?

Solutions Collecting From Web of "Undelete post (untrash)?"

There is not wp_undelete_post but you have other choices:

  • wp_untrash_post(): when post is trashed, the previous status is stored in _wp_trash_meta_status meta field. wp_untrash_post() restore trashed posts to previous status whatever it was; for example, private, inherit, publish. I’ve not tested it but it should work with a custom post status as well.
  • wp_publish_post(): if you want to move from trash to publish status.
  • wp_update_post(): to move from trash to any other status.

For example, for a given post ID (of any post type, including pages):

if( get_post_status( $post_ID ) == "trash" ) {
    wp_update_post( array(
                   'ID'           => $post_ID,
                   'post_status'  => 'publish'
               )
    );
}

or:

if( get_post_status( $post_ID ) == "trash" ) {
    wp_publish_post( $post_ID );
}

or:

if( get_post_status( $post_ID ) == "trash" ) {
    wp_untrash_post( $post_ID );
}

It might be old but it showed up when I was looking for similar problem.
Since WP 2.9.0 there is function called: wp_untrash_post(int $post_id)

In my case it worked like a charm.

the solution so far was:

$page =get_page_by_path('my-slug', OBJECT, 'page');
if($page && 'trash'==$page->post_status) { wp_update_post(array('ID'=>$page->ID,'post_status'=>'publish')); }