WordPress.org

Make WordPress Core

#38374 closed defect (bug) (fixed)

Null check needed for call to get_post_type_object() in get_post_type_archive_template()

Reported by: technopolitica Owned by: boonebgorges
Milestone: 4.8 Priority: normal
Severity: normal Version: 4.6.1
Component: Posts, Post Types Keywords: has-patch
Focuses: Cc:

Description

The get_post_type_archive_template() function defined in wp-includes/template.php:118 attempts to get the post type information for the current post via get_post_type_object() in order to check if the post type has an archive defined for it:

<?php
/* wp-includes/template.php:118 */
function get_post_type_archive_template() {
        $post_type = get_query_var( 'post_type' );
        if ( is_array( $post_type ) )
                $post_type = reset( $post_type );

        $obj = get_post_type_object( $post_type );
        if ( ! $obj->has_archive )
                return '';

        return get_archive_template();
}

However get_post_type_object may return null when the post_type query var is not defined which can happen e.g. with custom queries. Since get_post_type_archive_template() does not perform a null check against the return value of get_post_type_object() the call to $obj->has_archive in wp-includes/template.php on line 123 can trigger a "Trying to get property of non-object" error for these custom queries where 'post_type' is not set.

There should be a null-check on the return value of get_post_type_object() in the get_post_type_archive function, e.g.

<?php
        $obj = get_post_type_object( $post_type );
        if ( null === $obj || ! $obj->has_archive )
                return '';

Attachments (1)

38374.diff (460 bytes) - added by dlh 16 months ago.

Download all attachments as: .zip

Change History (6)

#1 @SergeyBiryukov
16 months ago

  • Component changed from General to Posts, Post Types
  • Keywords needs-patch added

@dlh
16 months ago

#2 @dlh
16 months ago

  • Keywords has-patch added; needs-patch removed

38374.diff would confirm that $obj is a post type object before accessing the property.

#3 @boonebgorges
16 months ago

  • Milestone changed from Awaiting Review to 4.8
  • Owner set to boonebgorges
  • Status changed from new to assigned

@technopolitica Thanks for the report, and welcome to WordPress Trac.

It looks like this issue dates back to the introduction of get_post_type_archive_template() in [25291]. get_post_type_object() has returned null since its introduction in [12597].

#4 @boonebgorges
16 months ago

I tried writing tests for this, but the use of constants in locate_template() makes get_post_type_archive_template() untestable. See #18298.

#5 @boonebgorges
16 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 40031:

Avoid PHP notices in get_post_type_archive_template().

The function should fail more gracefully when called in the context
where get_query_var( 'post_type' ) doesn't represent an actual post
type.

Props technopolitica, dlh.
Fixes #38374.

Note: See TracTickets for help on using tickets.