Is there a recover_post hook to go with trash_post hook?

I’m using the trash_post hook to set a flag in a custom table to indicate that this item is “deleted”, but when the user chooses to restore that post, what hook can I use for that? I couldn’t find anything on this page https://codex.wordpress.org/Plugin_API/Action_Reference, but maybe there’s another way to solve the problem.

Thanks!

Related posts

Leave a Reply

3 comments

  1. Looking at the code for WP 3.3.2, it seems that trash_post is actually wp_trash_post. From the wp_trash_post() function in /wp-includes/post.php:

    do_action('wp_trash_post', $post_id);
    
    add_post_meta($post_id,'_wp_trash_meta_status', $post['post_status']);
    add_post_meta($post_id,'_wp_trash_meta_time', time());
    
    $post['post_status'] = 'trash';
    wp_insert_post($post);
    
    wp_trash_post_comments($post_id);
    
    do_action('trashed_post', $post_id);
    

    So … I’d double check the hook you’re using to set your initial flag.

    However, there is a hook you can use to detect when a user restores a post. It’s aptly named untrash_post.

    Here it is in action from the same core file:

    function wp_untrash_post($post_id = 0) {
        if ( !$post = wp_get_single_post($post_id, ARRAY_A) )
            return $post;
    
        if ( $post['post_status'] != 'trash' )
            return false;
    
        do_action('untrash_post', $post_id);
    
        $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true);
    
        $post['post_status'] = $post_status;
    
        delete_post_meta($post_id, '_wp_trash_meta_status');
        delete_post_meta($post_id, '_wp_trash_meta_time');
    
        wp_insert_post($post);
    
        wp_untrash_post_comments($post_id);
    
        do_action('untrashed_post', $post_id);
    
        return $post;
    }