Using WordPress ‘pre_untrash_post’ PHP filter

The ‘pre_untrash_post’ filter allows you to modify or prevent the untrashing of a post in WordPress.

You can use this filter to decide if a post should be restored from the trash or not based on custom conditions.

Usage

add_filter( 'pre_untrash_post', 'my_custom_untrash_filter', 10, 3 );

function my_custom_untrash_filter( $untrash, $post, $previous_status ) {
    // Your custom logic here
    return $untrash;
}

Parameters

  • $untrash (bool|null): Determines whether to proceed with untrashing the post.
  • $post (WP_Post): The post object being untrashed.
  • $previous_status (string): The post status before it was trashed.

Examples

Prevent untrashing of specific post type

add_filter( 'pre_untrash_post', 'prevent_untrashing_custom_post_type', 10, 3 );

function prevent_untrashing_custom_post_type( $untrash, $post, $previous_status ) {
    if ( 'custom_post_type' === $post->post_type ) {
        return false;
    }

    return $untrash;
}

This example prevents untrashing of posts with a specific custom post type. If the post type matches ‘custom_post_type’, the untrashing process will be stopped.

Allow untrashing only for admins

add_filter( 'pre_untrash_post', 'allow_untrash_only_for_admins', 10, 3 );

function allow_untrash_only_for_admins( $untrash, $post, $previous_status ) {
    if ( ! current_user_can( 'manage_options' ) ) {
        return false;
    }

    return $untrash;
}

In this example, untrashing is allowed only for users with the ‘manage_options’ capability, which is typically assigned to administrators.

Send email notification when a post is untrashed

add_filter( 'pre_untrash_post', 'email_on_untrash_post', 10, 3 );

function email_on_untrash_post( $untrash, $post, $previous_status ) {
    $subject = "Post Untrashed: {$post->post_title}";
    $message = "The post '{$post->post_title}' has been untrashed.";

    wp_mail( '[email protected]', $subject, $message );

    return $untrash;
}

This example sends an email notification to the specified email address when a post is untrashed.

Log untrashing events

add_filter( 'pre_untrash_post', 'log_untrash_post', 10, 3 );

function log_untrash_post( $untrash, $post, $previous_status ) {
    error_log( "Post ID {$post->ID} with title '{$post->post_title}' has been untrashed." );

    return $untrash;
}

This example logs an event every time a post is untrashed. The log will contain the post ID and title.

Change post status after untrashing

add_filter( 'pre_untrash_post', 'change_status_after_untrashing', 10, 3 );

function change_status_after_untrashing( $untrash, $post, $previous_status ) {
    if ( 'draft' === $previous_status ) {
        add_action( 'untrashed_post', 'set_post_status_to_draft', 10, 1 );
        function set_post_status_to_draft( $post_id ) {
        wp_update_post(
            array(
                'ID'          => $post_id,
                'post_status' => 'draft',
            )
        );
    }
}

return $untrash;
}

In this example, if the previous status of the post was ‘draft’, the post status will be changed back to ‘draft’ after it has been untrashed. The `set_post_status_to_draft` function updates the post status to ‘draft’ using `wp_update_post`.