Canonical: Use `get_the_terms()` to verify that a post belongs to the requested `%category%`.

The `get_the_terms()` wrapper provides cache support, and saves a database hit
on sites with a persistent cache backend.

Props spacedmonkey.
Fixes #36602.

git-svn-id: https://develop.svn.wordpress.org/trunk@37262 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-04-20 15:34:15 +00:00
parent 01ef43ace7
commit 33c1cd9c10
1 changed files with 3 additions and 2 deletions

View File

@ -256,9 +256,10 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
}
} elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) {
$category = get_category_by_path( $cat );
$post_terms = wp_get_object_terms($wp_query->get_queried_object_id(), 'category', array('fields' => 'tt_ids'));
if ( (!$category || is_wp_error($category)) || ( !is_wp_error($post_terms) && !empty($post_terms) && !in_array($category->term_taxonomy_id, $post_terms) ) )
$post_terms = get_the_terms( $wp_query->get_queried_object_id(), 'category' );
if ( ( ! $category || is_wp_error( $category ) ) || ( ! is_wp_error( $post_terms ) && ! empty( $post_terms ) && ! in_array( $category, $post_terms ) ) ) {
$redirect_url = get_permalink($wp_query->get_queried_object_id());
}
}
// Post Paging