1

I’m getting overrun with these on my production site:

PHP Notice:  Trying to get property of non-object in /public_html/wp-includes/class-wp-query.php on line 3728
PHP Notice:  Trying to get property of non-object in /public_html/wp-includes/class-wp-query.php on line 3730
PHP Notice:  Trying to get property of non-object in /public_html/wp-includes/class-wp-query.php on line 3732
PHP Notice:  Trying to get property of non-object in /public_html/wp-includes/class-wp-query.php on line 3863

The above lines are in the is_page() and is_singular() functions.

I have no idea where these functions are being used incorrectly. What's frustrating is I can’t reproduce these on my staging site (same server as production) or on my localhost. And I can’t easily deactivate all plugins or switch themes because we have hundreds of signed in users and thousands of non-signed in users at any given time.

I have searched my repository for instances of is_page() and is_singular() and tried to verify that they are being called correctly. Clearly I have failed.

How do I figure out what's tripping these PHP Notices?

For the record, I browsed through the "similar questions" that popped up, before I submitted my question!

3

You can create your own custom error handler and add stack trace to your error log.

set_error_handler('wpse_288408_handle_error');

function wpse_288408_handle_error( $errno, $errstr, $errfile, $errline ) {

    if( $errno === E_USER_NOTICE ) {

        $message = 'You have an error notice: "%s" in file "%s" at line: "%s".' ;
        $message = sprintf($message, $errstr, $errfile, $errline);

        error_log($message);
        error_log(wpse_288408_generate_stack_trace());
    }
}
// Function from php.net http://php.net/manual/en/function.debug-backtrace.php#112238
function wpse_288408_generate_stack_trace() {

    $e = new \Exception();

    $trace = explode( "\n" , $e->getTraceAsString() );

    // reverse array to make steps line up chronologically

    $trace = array_reverse($trace);

    array_shift($trace); // remove {main}
    array_pop($trace); // remove call to this method

    $length = count($trace);
    $result = array();

    for ($i = 0; $i < $length; $i++) {
        $result[] = ($i + 1)  . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering
    }

    $result = implode("\n", $result);
    $result = "\n" . $result . "\n";

    return $result;
}

You can check if this is working by adding trigger_error somewhere in your code.

trigger_error('Annoying notice');

Your error log should output something like that:

2017/01/02 12:00:00 [error] 999#999: *999 FastCGI sent in stderr: "PHP message: You have an error notice: "Annoying notice" in file "/var/www/test/wp-content/plugins/test/test.php" at line: "99".
PHP message:
1) /var/www/test/index.php(17): require('/var/www/test/w...')
2) /var/www/test/wp-blog-header.php(13): require_once('/var/www/test/w...')
3) /var/www/test/wp-load.php(37): require_once('/var/www/test/w...')
4) /var/www/test/wp-config.php(93): require_once('/var/www/test/w...')
5) /var/www/test/wp-settings.php(305): include_once('/var/www/test/w...')
6) /var/www/test/wp-content/plugins/test/test.php(99): trigger_error('Annoying notice')
7) [internal function]: wpse_288408_handle_error(1024, 'Annoying notice', '/var/www/test/w...', 99, Array)" while reading response header from upstream, client: 192.168.33.1, server: test.dev, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9070", host: "test.dev"

With this kind of message it will be much easier to find out where the problem is.

1
  • 2
    To confirm: motivast's code worked. In my particular case the warning was coming from an RSS feed template. This was one reason Query Monitor, although very useful, did not help me out -- it doesn't run on RSS feeds.
    – Max
    Dec 14 '17 at 4:23
1

Stage and Prod may be on the same server but are they configured for the same version of PHP?

A Wordpress debug plugin might provide you with additional info. Query Monitor https://en-gb.wordpress.org/plugins/query-monitor/#description provides a call stack for PHP Notices and Errors. After activation: as Admin User, go to an offending page. Not obvious, but the ribbon at the top should display seconds and KB, hover over this and click errors (I think). On my site (only a notice - no errors):

Query Monitor ribbon

Once you click Errors, details will be displayed including a call stack which might help.

If PHP 7 is an issue - there is a compatibility checker somewhere in the WP Plugin directory which might be useful. It does give false positives e.g. a plugin may be marked incompatible even if it only uses a depracated function after checking PHP 7 is not being used by server.

1
  • Thanks scytale. This is a solid suggestion. I did have query monitor installed previously. The problem is that I don't know where the offending page is. :-( Thanks for pointing out the possibility that the PHP version is different. I actually wasn't sure, so I checked it. For my host, that is a server level setting, not application level. So both staging and production are PHP7.0.
    – Max
    Dec 13 '17 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.