Halloween Sale 15% off Ends in See the plans
PHP

Redirect Trashed Posts to Their Category

Remembers the address of each post or page you move to the trash and sends later visitors to its category, parent page or the blog page instead of a 404, even after the trash is emptied.

Don’t have WPIDE yet? Install it on your site

Code

// Post types to remember; when one is trashed, its old address leads to its first category (posts)
// or its parent page (pages), and otherwise to the blog page.
$wpide_removed_types = array( 'post', 'page' );
// How many old addresses to remember (oldest are dropped first).
$wpide_removed_limit = 500;

add_action( 'wp_trash_post', function ( $post_id ) use ( $wpide_removed_types, $wpide_removed_limit ) {
    $post = get_post( $post_id );

    if ( ! $post || 'publish' !== $post->post_status || ! in_array( $post->post_type, $wpide_removed_types, true ) ) {
        return;
    }

    $target = '';

    if ( 'post' === $post->post_type ) {
        $cats   = wp_get_post_categories( $post->ID );
        $target = $cats ? get_category_link( (int) $cats[0] ) : '';
    } elseif ( $post->post_parent && 'publish' === get_post_status( $post->post_parent ) ) {
        $target = get_permalink( $post->post_parent );
    }

    if ( ! $target ) {
        $blog   = (int) get_option( 'page_for_posts' );
        $target = $blog ? get_permalink( $blog ) : home_url( '/' );
    }

    $path = untrailingslashit( (string) wp_parse_url( get_permalink( $post ), PHP_URL_PATH ) );
    $map  = (array) get_option( 'wpide_removed_post_redirects', array() );

    unset( $map[ $path ] );
    $map[ $path ] = array( 'to' => $target, 'id' => $post->ID );

    update_option( 'wpide_removed_post_redirects', array_slice( $map, -absint( $wpide_removed_limit ), null, true ), false );
} );

// Restoring a post from the trash removes its redirect.
add_action( 'untrashed_post', function ( $post_id ) {
    $map = (array) get_option( 'wpide_removed_post_redirects', array() );

    foreach ( $map as $path => $item ) {
        if ( isset( $item['id'] ) && (int) $item['id'] === (int) $post_id ) {
            unset( $map[ $path ] );
        }
    }

    update_option( 'wpide_removed_post_redirects', $map, false );
} );

add_action( 'template_redirect', function () {
    if ( ! is_404() ) {
        return;
    }

    $map  = get_option( 'wpide_removed_post_redirects', array() );
    $path = isset( $_SERVER['REQUEST_URI'] ) ? untrailingslashit( (string) wp_parse_url( wp_unslash( $_SERVER['REQUEST_URI'] ), PHP_URL_PATH ) ) : '';

    if ( ! is_array( $map ) || '' === $path || empty( $map[ $path ]['to'] ) ) {
        return;
    }

    wp_safe_redirect( $map[ $path ]['to'], 301 );
    exit;
} );

Notes

Restoring a post removes its redirect; up to 500 addresses are remembered.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.