[wp-trac] [WordPress Trac] #49024: Notice: Trying to get property XX of non-object in WP_Query Class
WordPress Trac
noreply at wordpress.org
Wed Dec 18 07:42:44 UTC 2019
#49024: Notice: Trying to get property XX of non-object in WP_Query Class
------------------------------+-----------------------------
Reporter: agengineering | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version:
Severity: normal | Keywords:
Focuses: coding-standards |
------------------------------+-----------------------------
Hi there,
sometimes the function
{{{
is_page()
}}}
return some notice like this:
{{{
Notice: Trying to get property ‘ID’ of non-object in
/home/talabiah/public_html/wp-includes/class-wp-query.php on line 3965
Notice: Trying to get property ‘post_title’ of non-object in
/home/talabiah/public_html/wp-includes/class-wp-query.php on line 3967
}}}
The reason is that the queried object is null and we use it without any
kind of check.
Look here:
1. First step: call function is_page():
[[Image(01.png )]]
wp-includes/query.php:566
2. is_page call the method is_page() of WP_Query class, look the file wp-
includes/class-wp-query.php:3952
{{{
WP_Query::is_page() call WP_Query::get_queried_object()
}}}
[[Image(02.png )]]
3. get_queired_object() method hae a set of if-elseif but we are not sure
that the default value:
{{{
$this->queried_object = null;
}}}
will be change. There other functions like get_post() or get_userdata()
that can return null/false value.
This value will return to WP_Query::is_page()
[[Image(03.png )]]
wp-includes/class-wp-query.php:3427
4. In WP_Query::is_page() there aren't any check if this value is an
object, but we use it like if we are sure that the get_queried_object()
can return ONLY an object.
[[Image(04.png )]]
I think the best solution is to check if $page_obj is empty or not. A
solution can be this:
{{{#!php
<?php
/**
* Is the query for an existing single page?
*
* If the $page parameter is specified, this function will
additionally
* check if the query is for one of the pages specified.
*
* @see WP_Query::is_single()
* @see WP_Query::is_singular()
*
* @since 3.1.0
*
* @param int|string|array $page Optional. Page ID, title, slug,
path, or array of such. Default empty.
* @return bool Whether the query is for an existing single page.
*/
public function is_page( $page = '' ) {
if ( ! $this->is_page ) {
return false;
}
if ( empty( $page ) ) {
return true;
}
$page_obj = $this->get_queried_object();
$page = array_map( 'strval', (array) $page );
if ( ! empty( $page_obj ) && in_array( (string)
$page_obj->ID, $page ) ) {
return true;
} elseif ( ! empty( $page_obj ) && in_array(
$page_obj->post_title, $page ) ) {
return true;
} elseif ( ! empty( $page_obj ) && in_array(
$page_obj->post_name, $page ) ) {
return true;
} else {
foreach ( $page as $pagepath ) {
if ( ! strpos( $pagepath, '/' ) ) {
continue;
}
$pagepath_obj = get_page_by_path(
$pagepath );
if ( $pagepath_obj && ( $pagepath_obj->ID
== $page_obj->ID ) ) {
return true;
}
}
}
return false;
}
?>
}}}
Thanks for your attention.
Pleae note: I have found a similar reporting here:
[https://core.trac.wordpress.org/ticket/29660 #29660]
--
Ticket URL: <https://core.trac.wordpress.org/ticket/49024>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list