You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中Trying to get property of non-object报错排查求助

Fixing "Trying to get property of non-object" Notices in WordPress's WP_Query

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_results filter to convert WP_Post objects to arrays, accessing properties with -> will break instantly.
    • A misconfigured pre_get_posts hook might be altering the query to return non-post items, or filtering out posts in a way that leaves null values in the $posts array.
  • Add temporary debug code to inspect the issue
    To see exactly what's in the $posts array when the error hits, add a quick check right before the problematic line in class-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_Post objects 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
      }
      

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:21:32