Make WordPress Core


Ignore:
Timestamp:
08/08/2016 06:48:53 PM (8 years ago)
Author:
boonebgorges
Message:

Improve category check in redirect_canonical() when permastruct contains category slug.

[37262] changed a check in redirect_canonical() so that it checked
categories in the object cache rather than querying the database. However,
the check was based on the identity of WP_Term objects, which in
certain cases can be augmented by the main WP query routine, causing
failures of the in_array() check. This caused unnecessary redirects
for URLs where is_single() is true, but the URL is different from the
post permalink, such as the embed endpoint.

has_term() also checks the cache, but does not sufer from this bug.

Props cmillerdev.
Fixes #36602.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/canonical.php

    r38107 r38216  
    257257        } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) {
    258258            $category = get_category_by_path( $cat );
    259             $post_terms = get_the_terms( $wp_query->get_queried_object_id(), 'category' );
    260             if ( ( ! $category || is_wp_error( $category ) ) || ( ! is_wp_error( $post_terms ) && ! empty( $post_terms ) && ! in_array( $category, $post_terms ) ) ) {
     259            if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) {
    261260                $redirect_url = get_permalink($wp_query->get_queried_object_id());
    262261            }
Note: See TracChangeset for help on using the changeset viewer.