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.
Built from https://develop.svn.wordpress.org/trunk@37262


git-svn-id: http://core.svn.wordpress.org/trunk@37228 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2016-04-20 15:35:27 +00:00
parent 9d9c6f3016
commit c8325e1ab5
2 changed files with 4 additions and 3 deletions

View File

@ -256,10 +256,11 @@ 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
if ( is_singular() && get_query_var('page') ) {

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.6-alpha-37261';
$wp_version = '4.6-alpha-37262';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.