[wp-trac] [WordPress Trac] #38374: Null check needed for call to get_post_type_object() in get_post_type_archive_template()

WordPress Trac noreply at wordpress.org
Wed Oct 19 20:41:47 UTC 2016


#38374: Null check needed for call to get_post_type_object() in
get_post_type_archive_template()
----------------------------+-----------------------------
 Reporter:  technopolitica  |      Owner:
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  General         |    Version:  4.6.1
 Severity:  normal          |   Keywords:
  Focuses:                  |
----------------------------+-----------------------------
 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
 <?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
 <?php
         $obj = get_post_type_object( $post_type );
         if ( null === $obj || ! $obj->has_archive )
                 return '';
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/38374>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list