3

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
    The best way would be to use a debugger like xdebug but a fast and easy way could be to add the line error_log(print_r(debug_backtrace(), true)); at the plae where the notice appears. With this you manually add a stack trace to the error log. – ahendwh2 Mar 8 '18 at 10:35
  • @ahendwh2 Thanks, 1) I looked in to xedebug but it requires a new version of PHP than I have. 2) "At the place where the notice appears" - where is this? I mean, if I knew where it appeared, I'd know more. Output refs class-wp-query.php and pluggable.php, but I wouldn't know where to place the debug line outside of these. The plugin I most want to test has no front-end. – Robert Andrews Mar 8 '18 at 10:38
  • You should place the debug line in class-wp-query.php right after (or before) line 3736 – ahendwh2 Mar 8 '18 at 10:47
  • Okay, adding error_log(print_r(debug_backtrace(), true)); to class-wp-query.php (whether before or after line 3736) adds several 999-line entries to debug.log, spitting out an array (and arrays within arrays). It all seems to correlate with page ID 2, which is my homepage. But I don't know what I'm looking for; it doesn't seem to state any errors. It didn't really add a more detailed stack trace as I understand it, just the arrays. – Robert Andrews Mar 8 '18 at 11:15
  • Hmm. Have you tried to deactivate one plugin after another and see, wether the error persists? – ahendwh2 Mar 8 '18 at 11:49
1

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!

  • I earlier installed and enabled xdebug to uncover more detail. I'm currently unable to test whether your answer works, though it looks like a great solution to use in lieu of xdebug. – Robert Andrews Apr 9 at 13:04
  • 1
    Xdebug is always the best solution but this does work for me. If you do get a chance to test it and it does solve your problem it would be great if you could select it as the answer. Thanks. – Ted Stresen-Reuter Apr 10 at 14:14
  • Certainly will. – Robert Andrews Apr 13 at 8:04

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.