mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-02 08:10:04 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@25291 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -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();
|
||||
|
||||
|
||||
@@ -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 );
|
||||
}
|
||||
|
||||
@@ -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() ) :
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -648,4 +648,39 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
|
||||
$this->go_to( '/2013/11/41/' );
|
||||
$this->assertQueryTrue( 'is_404' );
|
||||
}
|
||||
|
||||
function test_post_type_archive_with_tax_query() {
|
||||
delete_option( 'rewrite_rules' );
|
||||
|
||||
$cpt_name = 'ptawtq';
|
||||
register_post_type( $cpt_name, array(
|
||||
'taxonomies' => array( 'post_tag', 'category' ),
|
||||
'rewrite' => true,
|
||||
'has_archive' => true,
|
||||
'public' => true
|
||||
) );
|
||||
|
||||
$tag_id = $this->factory->tag->create( array( 'slug' => 'tag-slug' ) );
|
||||
$post_id = $this->factory->post->create( array( 'post_type' => $cpt_name ) );
|
||||
wp_set_object_terms( $post_id, $tag_id, 'post_tag' );
|
||||
|
||||
$this->go_to( '/ptawtq/' );
|
||||
$this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
|
||||
$this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
|
||||
|
||||
add_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_tax_query' ) );
|
||||
|
||||
$this->go_to( '/ptawtq/' );
|
||||
$this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
|
||||
$this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
|
||||
|
||||
remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_tax_query' ) );
|
||||
}
|
||||
|
||||
function pre_get_posts_with_tax_query( &$query ) {
|
||||
$term = get_term_by( 'slug', 'tag-slug', 'post_tag' );
|
||||
$query->set( 'tax_query', array(
|
||||
array( 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $term->term_id )
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user