Opened 8 years ago
Closed 8 years ago
#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 '';
38374.diff would confirm that
$obj
is a post type object before accessing the property.