From a67d551daca72c0cd406eb47a884da04cbe4f2b3 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 6 Sep 2013 22:07:09 +0000 Subject: [PATCH] Ensure that the post type object is the queried object when a post type has been registered with `has_archive => true`. Ensure it is not stomped when decorated with `tax_query`. Adds unit tests. Props nacin. Fixes #18614. Built from https://develop.svn.wordpress.org/trunk@25291 git-svn-id: http://core.svn.wordpress.org/trunk@25255 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/general-template.php | 19 +++++++++++++++---- wp-includes/query.php | 4 ++-- wp-includes/template-loader.php | 1 + wp-includes/template.php | 15 +++++++++++++++ 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php index 7fb4485fec..598ccefdf4 100644 --- a/wp-includes/general-template.php +++ b/wp-includes/general-template.php @@ -577,6 +577,13 @@ function wp_title($sep = '»', $display = true, $seplocation = '') { $title = single_post_title( '', false ); } + // If there's a post type archive + if ( is_post_type_archive() ) { + $post_type_object = get_post_type_object( get_query_var( 'post_type' ) ); + if ( ! $post_type_object->has_archive ) + $title = post_type_archive_title( '', false ); + } + // If there's a category or tag if ( is_category() || is_tag() ) { $title = single_term_title( '', false ); @@ -595,8 +602,8 @@ function wp_title($sep = '»', $display = true, $seplocation = '') { $title = $author->display_name; } - // If there's a post type archive - if ( is_post_type_archive() ) + // Post type archives with has_archive should override terms. + if ( is_post_type_archive() && $post_type_object->has_archive ) $title = post_type_archive_title( '', false ); // If there's a month @@ -696,7 +703,7 @@ function post_type_archive_title( $prefix = '', $display = true ) { if ( ! is_post_type_archive() ) return; - $post_type_obj = get_queried_object(); + $post_type_obj = get_post_type_object( get_query_var( 'post_type' ) ); $title = apply_filters('post_type_archive_title', $post_type_obj->labels->name ); if ( $display ) @@ -1670,7 +1677,7 @@ function feed_links_extra( $args = array() ) { $args = wp_parse_args( $args, $defaults ); - if ( is_single() || is_page() ) { + if ( is_singular() ) { $id = 0; $post = get_post( $id ); @@ -1678,6 +1685,10 @@ function feed_links_extra( $args = array() ) { $title = sprintf( $args['singletitle'], get_bloginfo('name'), $args['separator'], esc_html( get_the_title() ) ); $href = get_post_comments_feed_link( $post->ID ); } + } elseif ( is_post_type_archive() ) { + $post_type_obj = get_post_type_object( get_query_var( 'post_type' ) ); + $title = sprintf( $args['posttypetitle'], get_bloginfo( 'name' ), $args['separator'], $post_type_obj->labels->name ); + $href = get_post_type_archive_feed_link( $post_type_obj->name ); } elseif ( is_category() ) { $term = get_queried_object(); diff --git a/wp-includes/query.php b/wp-includes/query.php index 8da7e68f09..43be546c35 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -3147,10 +3147,10 @@ class WP_Query { * @return bool */ function is_post_type_archive( $post_types = '' ) { - if ( empty( $post_types ) || !$this->is_post_type_archive ) + if ( empty( $post_types ) || ! $this->is_post_type_archive ) return (bool) $this->is_post_type_archive; - $post_type_object = $this->get_queried_object(); + $post_type_object = get_post_type_object( $this->get( 'post_type' ) ); return in_array( $post_type_object->name, (array) $post_types ); } diff --git a/wp-includes/template-loader.php b/wp-includes/template-loader.php index 7051f3452b..81de95618f 100644 --- a/wp-includes/template-loader.php +++ b/wp-includes/template-loader.php @@ -26,6 +26,7 @@ if ( defined('WP_USE_THEMES') && WP_USE_THEMES ) : $template = false; if ( is_404() && $template = get_404_template() ) : elseif ( is_search() && $template = get_search_template() ) : + elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) : elseif ( is_tax() && $template = get_taxonomy_template() ) : elseif ( is_front_page() && $template = get_front_page_template() ) : elseif ( is_home() && $template = get_home_template() ) : diff --git a/wp-includes/template.php b/wp-includes/template.php index 5bee8ad52d..78557e8a48 100644 --- a/wp-includes/template.php +++ b/wp-includes/template.php @@ -72,6 +72,21 @@ function get_archive_template() { return get_query_template( 'archive', $templates ); } +/** + * Retrieve path of post type archive template in current or parent template. + * + * @since 3.7.0 + * + * @return string + */ +function get_post_type_archive_template() { + $obj = get_post_type_object( get_query_var( 'post_type' ) ); + if ( ! $obj->has_archive ) + return ''; + + return get_archive_template(); +} + /** * Retrieve path of author template in current or parent template. *