How to 301 private posts rather than 404?

Sorry guys, I found my answer:

add_action('wp','redirect_stuffs', 0);
function redirect_stuffs(){
global $wpdb; 
    if ($wpdb->last_result[0]->post_status == "private" && !is_admin() ):
        wp_redirect( home_url(), 301 );
        exit();
    endif;
}

Posts/Pages are removed from the sitemaps, but the page still shows up on the site so that it can get 301’d.

Leave a Comment