wp_old_slug_redirect [ WordPress Function ]
wp_old_slug_redirect ( No parameters )
| Uses: |
|
| Returns: |
|
| Defined at: |
|
Soorgelijke functies: wp_safe_redirect, wp_redirect, wp_validate_redirect, is_redirect, wp_sanitize_redirect
Redirect old slugs to the correct permalink.
Attempts to find the current slug from the past slugs.
Source
<?php
function wp_old_slug_redirect() {
global $wp_query;
if ( is_404() && '' != $wp_query->query_vars['name'] ) :
global $wpdb;
// Guess the current post_type based on the query vars.
if ( get_query_var('post_type') )
$post_type = get_query_var('post_type');
elseif ( !empty($wp_query->query_vars['pagename']) )
$post_type = 'page';
else
$post_type = 'post';
if ( is_array( $post_type ) ) {
if ( count( $post_type ) > 1 )
return;
$post_type = array_shift( $post_type );
}
// Do not attempt redirect for hierarchical post types
if ( is_post_type_hierarchical( $post_type ) )
return;
$query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, $wp_query->query_vars['name']);
// if year, monthnum, or day have been specified, make our query more precise
// just in case there are multiple identical _wp_old_slug values
if ( '' != $wp_query->query_vars['year'] )
$query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
if ( '' != $wp_query->query_vars['monthnum'] )
$query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
if ( '' != $wp_query->query_vars['day'] )
$query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
$id = (int) $wpdb->get_var($query);
if ( ! $id )
return;
$link = get_permalink($id);
if ( !$link )
return;
wp_redirect( $link, 301 ); // Permanent redirect
exit;
endif;
}
?>
Examples [ wp-snippets.com ]
Top Google zoekresultaten
- Function Reference/wp old slug redirect « WordPress Codex
Description. Redirect old slugs to the correct permalink. Attempts to find the current slug from the past slugs. Usage. <?php wp_old_slug_redirect() ?> ...
codex.wordpress.org - Docs for page query.php
null wp_old_slug_redirect (). wp_reset_postdata (line 118). After looping through a separate query, this function restores the $post global to the current post in ...
phpdoc.wordpress.org - #16949 (Custom post types: error on page if query contains multiple ...
It looks like this is caused by the call to is_post_type_hierarchical() in wp_old_slug_redirect(). It just passes through what it gets from get_query_var(' post_type') ...
core.trac.wordpress.org - wp_old_slug_redirect
Function and Method Cross Reference. wp_old_slug_redirect(). Defined at: /wp- includes/query.php -> line 3520. No references found.
phpxref.ftwr.co.uk