WordPress中Trying to get property of non-object报错排查求助
Hey there! Let's work through those frustrating "Trying to get property of non-object" notices you're hitting. I know the gist of the error, but pinpointing the trigger can be tricky—let's break this down step by step.
First, a quick recap: This notice pops up when you try to access a property (like ->post_parent) on something that isn't an object. In your case, it's happening in wp-includes/class-wp-query.php (around line 4029 in the latest core code), which means the WP_Query instance is expecting a WP_Post object in its $posts array, but it's getting something else—usually null, an array, or a non-post object.
Here's how to track down and fix the issue:
Narrow down the trigger scenario
When do these notices appear? Is it on a specific page, when loading the admin dashboard, or only when a certain feature is used? Knowing the context will help you zero in on which query is failing.Check custom queries and filter hooks
WordPress has tons of hooks that modify query results, and it's common for custom code or plugins to mess this up. For example:- If you (or a plugin) use the
posts_resultsfilter to convertWP_Postobjects to arrays, accessing properties with->will break instantly. - A misconfigured
pre_get_postshook might be altering the query to return non-post items, or filtering out posts in a way that leavesnullvalues in the$postsarray.
- If you (or a plugin) use the
Add temporary debug code to inspect the issue
To see exactly what's in the$postsarray when the error hits, add a quick check right before the problematic line inclass-wp-query.php(remember to remove this after debugging!):// Right before line 4029 (adjust line number if needed) if ( isset( $this->posts[ $i ] ) && ! is_object( $this->posts[ $i ] ) ) { // Log the non-object value and query args to your PHP error log error_log( 'Non-object found in WP_Query posts array: ' . print_r( $this->posts[ $i ], true ) ); error_log( 'Corresponding query arguments: ' . print_r( $this->query_vars, true ) ); }Check your server's PHP error log afterward—this will show you exactly what's wrong with the post item and which query is causing it.
Test with default theme and no plugins
Switch to a default WordPress theme (like Twenty Twenty-Four) and disable all third-party plugins. If the notices disappear, re-enable them one by one (or switch back to your theme) to find the culprit. This is the fastest way to rule out theme/plugin conflicts.Fix the root cause
Once you've found the source:- If it's a custom filter hook returning arrays instead of objects, modify it to keep
WP_Postobjects intact. - If a query is returning non-post items, adjust the query arguments to only fetch valid posts.
- Always add a check before accessing object properties to prevent future notices:
if ( is_object( $post ) ) { $parent_id = $post->post_parent; // Safe to access now } else { $parent_id = 0; // Fallback value }
- If it's a custom filter hook returning arrays instead of objects, modify it to keep
Remember, never leave debug code in core WordPress files long-term—once you've fixed the issue, revert those changes to keep core files clean.
内容的提问来源于stack exchange,提问作者kanlukasz

