你所指的魔法发生在redirect_canonical()
. 更具体地说,redirect_guess_404_permalink()
.
你可以提前切入redirect_canonical
通过钩住template_redirect
优先级稍高的操作-在这种情况下,任何低于10
.
function wpse_145210_redirect_canonical() {
if ( is_404() && $name = get_query_var( \'name\' ) ) {
// The following is straight from redirect_guess_404_permalink(), do whatever you want instead!
$where = $wpdb->prepare("post_name LIKE %s", like_escape( $name ) . \'%\');
// if any of post_type, year, monthnum, or day are set, use them to refine the query
if ( get_query_var(\'post_type\') )
$where .= $wpdb->prepare(" AND post_type = %s", get_query_var(\'post_type\'));
else
$where .= " AND post_type IN (\'" . implode( "\', \'", get_post_types( array( \'public\' => true ) ) ) . "\')";
if ( get_query_var(\'year\') )
$where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var(\'year\'));
if ( get_query_var(\'monthnum\') )
$where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var(\'monthnum\'));
if ( get_query_var(\'day\') )
$where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var(\'day\'));
$post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = \'publish\'");
if ( ! $post_id )
return false;
if ( get_query_var( \'feed\' ) )
$url = get_post_comments_feed_link( $post_id, get_query_var( \'feed\' ) );
elseif ( get_query_var( \'page\' ) )
$url = trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( \'page\' ), \'single_paged\' );
else
$url = get_permalink( $post_id );
wp_redirect( $url, 301 );
exit;
}
}
add_action( \'template_redirect\', \'wpse_145210_redirect_canonical\', 5 );