WordPress.org

Make WordPress Core

Opened 2 weeks ago

#44319 new defect (bug)

Trying to get property of non-object PHP notice will occurs if value returned from get_queried_object() is null or empty.

Reported by: calmiraged88 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.6
Component: Query Keywords: dev-feedback has-patch
Focuses: template Cc:

Description

In my case, I tryin to set wpquery's "author_name" array value to "" on Author page, so it'd returning all posts from every users. (because for several reason and development awesomeness, i need this)

<?php
function test_pre_get_posts($query) {

  if ($query->is_author && $query->is_main_query()) {
    $query->set('author_name', '');
  }

  return $query;
}
add_filter('pre_get_posts','test_pre_get_posts');
?>

horray, now it's returning the stuffs all i wanted. but hol'up! now this ugly creature appears: "Notice: Trying to get property of non-object in C:\xampp\htdocs\wp-includes\class-wp-query.php on line ..."

let's put aside the line of the codes, here is where the problem happened. on public function is_author( ... ):

$author_obj = $this->get_queried_object();

$author = array_map( 'strval', (array) $author );

if ( in_array( (string) $author_obj->ID, $author ) )
  return true;
elseif ( in_array( $author_obj->nickname, $author ) )
  return true;
elseif ( in_array( $author_obj->user_nicename, $author ) )
  return true;

return false;

cool stuff, but could you guys give another validation for this thing like uhh

if (empty($author_obj->ID))
  return false;

or something above those conditional stuff that returns true? so it wouldn't produce error notice as I mentioned before.

please

Change History (0)

Note: See TracTickets for help on using tickets.