How to find cause of PHP notices with no stack trace?

My WordPress debug.log is filling up with this group of seven PHP warnings/notices, recurring at least every half an hour…

[08-Mar-2018 09:05:03 UTC] PHP Notice:  Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3736
[08-Mar-2018 09:05:03 UTC] PHP Notice:  Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3738
[08-Mar-2018 09:05:03 UTC] PHP Notice:  Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3740
[08-Mar-2018 09:05:03 UTC] PHP Notice:  Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3736
[08-Mar-2018 09:05:03 UTC] PHP Notice:  Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3738
[08-Mar-2018 09:05:03 UTC] PHP Notice:  Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3740
[08-Mar-2018 09:05:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/mysite/public_html/wp-includes/class-wp-query.php:3736) in /home/mysite/public_html/wp-includes/pluggable.php on line 1216

The trouble is, as far as I can see, there is no reference to which plugin or culprit is causing this (I think the term for this is, there is no back trace / stack trace), so I’m finding it hard to debug.

The question is: How can I find out more detail in order to trace the cause?

I already have WP_DEBUG, WP_DEBUG and WP_DEBUG_DISPLAY all set to true.

I have looked in to setting PHP display_errors and error_reporting but, since I am already getting notices and warnings output, I am not sure whether these settings would add any more detail.

Is it possible these warnings are being generated by a non-WordPress plugin? I do have a PHP script operating on cron which invokes the wpdb environment but which is not strictly a plugin.

2 s
2

You can get the backtrace by trapping the E_NOTICE message. If you add the following snippet to wp-config.php just before

/* That's all, stop editing! Happy blogging. */

you should see the full backtrace in the error log.

set_error_handler(function() {
    error_log(print_r(debug_backtrace(), true));
    return true;
}, E_USER_NOTICE);

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );

/* That's all, stop editing! Happy blogging. */

PS: Returning true at the end of the function tells PHP to stop processing the E_NOTICE, which is probably what you want at this point.

I just used this approach on a project and it was exactly the solution I was looking for. In fact, I found this post while looking for a way to do the same thing. Happy sailing!

Leave a Comment