WordPress.org

Make WordPress Core

#44319 closed defect (bug) (duplicate)

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: Priority: normal
Severity: normal Version: 4.9.6
Component: Query Keywords:
Focuses: template Cc:
PR Number:

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 (1)

#1 @SergeyBiryukov
13 months ago

  • Keywords dev-feedback has-patch removed
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi there, welcome to WordPress Trac!

Thanks for the report, we're already tracking this issue in #29660.

Note: See TracTickets for help on using tickets.