-
-
Save maciejbis/6fde3381a29f2e224640aa4953d9a929 to your computer and use it in GitHub Desktop.
Redirect from 404
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function pm_redirect_posts_from_404() { | |
global $wp, $wpdb, $wp_query, $pm_query; | |
if(is_404()) { | |
$slug = basename($wp->request); | |
$element = $wpdb->get_row($wpdb->prepare(" | |
SELECT * FROM {$wpdb->posts} | |
WHERE post_name = %s | |
AND post_status = 'publish' | |
", | |
$slug | |
)); | |
if(!empty($element->ID)) { | |
$url = get_permalink($element->ID); | |
} /*else { | |
$element = $wpdb->get_row($wpdb->prepare(" | |
SELECT * FROM {$wpdb->terms} | |
LEFT JOIN {$wpdb->term_taxonomy} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id | |
WHERE slug = %s", | |
$slug | |
)); | |
if(!empty($element->term_id)) { | |
$url = get_term_link((int) $element->term_id, $element->taxonomy); | |
} | |
} */ | |
if(!empty($url)) { | |
wp_safe_redirect($url, 301); | |
exit(); | |
} | |
} | |
} | |
add_action('template_redirect', 'pm_redirect_posts_from_404', 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment