The magic you’re referring to happens in redirect_canonical()
. And more specifically, redirect_guess_404_permalink()
.
You can cut in ahead of redirect_canonical
by hooking onto the template_redirect
action with a slightly higher priority – in this case, anything lower than 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 );