Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brokenfunction/9e221198d6bb71197e6b2e2210c69494 to your computer and use it in GitHub Desktop.
Save brokenfunction/9e221198d6bb71197e6b2e2210c69494 to your computer and use it in GitHub Desktop.
301 Redirect trashed posts to the home page instead of a 404 page
add_action('template_redirect', 'trash_redirect');
function trash_redirect(){
if (is_404()){
global $wp_query, $wpdb;
$vacancyName = $wp_query->query['name'];
$deletedVacancies = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'cpt_vacancies' AND post_name like '%$vacancyName%' AND post_status = 'trash' LIMIT 1");
if(count($deletedVacancies) > 0) {
wp_redirect(home_url(), 301);
die();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment