From bbbf6eab8a2e744e34277031b696613abfa2645d Mon Sep 17 00:00:00 2001
From: Dion Hulse <dd32@git.wordpress.org>
Date: Wed, 13 Jan 2016 08:19:48 +0000
Subject: [PATCH] Canonical / Query: Restore the `is_404()` check in
 `wp_old_slug_redirect()` which was removed in [34659]. This reverts part of
 [34659] due to excessive canonical problems it's caused in 4.4.x.

Fixes #35344, #21602 for the 4.4 branch.


git-svn-id: https://develop.svn.wordpress.org/branches/4.4@36280 602fd350-edb4-49c9-b593-d223f7449a82
---
 src/wp-includes/query.php | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)

diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php
index 9460d32450..6af7501e09 100644
--- a/src/wp-includes/query.php
+++ b/src/wp-includes/query.php
@@ -4981,16 +4981,11 @@ class WP_Query {
  *
  * @global WP_Query   $wp_query   Global WP_Query instance.
  * @global wpdb       $wpdb       WordPress database abstraction object.
- * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  */
 function wp_old_slug_redirect() {
-	global $wp_query, $wp_rewrite;
+	global $wp_query;
 
-	if ( get_queried_object() ) {
-		return;
-	}
-
-	if ( '' !== $wp_query->query_vars['name'] ) :
+	if ( is_404() && '' !== $wp_query->query_vars['name'] ) :
 		global $wpdb;
 
 		// Guess the current post_type based on the query vars.
@@ -5038,13 +5033,6 @@ function wp_old_slug_redirect() {
 			$link = user_trailingslashit( trailingslashit( $link ) . 'page/' . $GLOBALS['wp_query']->query_vars['paged'] );
 		} elseif( is_embed() ) {
 			$link = user_trailingslashit( trailingslashit( $link ) . 'embed' );
-		} elseif ( is_404() ) {
-			// Add rewrite endpoints if necessary.
-			foreach ( $wp_rewrite->endpoints as $endpoint ) {
-				if ( $endpoint[2] && false !== get_query_var( $endpoint[2], false ) ) {
-					$link = user_trailingslashit( trailingslashit( $link ) . $endpoint[1] );
-				}
-			}
 		}
 
 		/**