diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index ac8864334a..701af8de4a 100644 --- a/src/wp-includes/post-template.php +++ b/src/wp-includes/post-template.php @@ -1631,6 +1631,10 @@ function get_the_password_form( $post = 0 ) { * @return bool True on success, false on failure. */ function is_page_template( $template = '' ) { + if ( ! is_singular() ) { + return false; + } + $page_template = get_page_template_slug( get_queried_object_id() ); if ( empty( $template ) ) diff --git a/tests/phpunit/tests/query/conditionals.php b/tests/phpunit/tests/query/conditionals.php index 5085052d69..8cceaa54eb 100644 --- a/tests/phpunit/tests/query/conditionals.php +++ b/tests/phpunit/tests/query/conditionals.php @@ -1073,6 +1073,32 @@ class Tests_Query_Conditionals extends WP_UnitTestCase { $this->assertTrue( is_page_template( array( 'test.php', 'example.php' ) ) ); } + /** + * @ticket 39211 + */ + function test_is_page_template_not_singular() { + global $wpdb; + + // We need a non-post that shares an ID with a post assigned a template. + $user_id = self::factory()->user->create(); + if ( ! get_post( $user_id ) ) { + $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) ); + $wpdb->update( $wpdb->posts, array( 'ID' => $user_id ), array( 'ID' => $post_id ), array( '%d' ) ); + } + + update_post_meta( $user_id, '_wp_page_template', 'example.php' ); + + // Verify that the post correctly reports having a template. + $this->go_to( get_post_permalink( $user_id ) ); + $this->assertInstanceOf( 'WP_Post', get_queried_object() ); + $this->assertTrue( is_page_template( 'example.php' ) ); + + // Verify that the non-post with a matching ID does not report having a template. + $this->go_to( get_author_posts_url( $user_id ) ); + $this->assertInstanceOf( 'WP_User', get_queried_object() ); + $this->assertFalse( is_page_template( 'example.php' ) ); + } + /** * @ticket 35902 */