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

求助:WordPress中/wp-includes/post-template.php第298行非对象属性错误修复无效

Fixing "Trying to get property of non-object" in post-template.php Line 298

Hey there, let's break down why you're hitting this error and what you can do beyond just calling wp_reset_postdata().

What's Causing This?

That notice pops up because the $post variable being passed to functions like post_password_required() (in the code snippet you shared) isn't a valid WP_Post object. This usually happens when:

  • A custom query messed up the global $post state, and the reset wasn't done correctly.
  • You're calling template functions like get_the_content() outside of a proper loop, where the global $post isn't initialized.
  • A plugin or theme snippet accidentally overwrote or unset the $post variable.

Why wp_reset_postdata() Might Not Be Working

First, let's clarify two easy-to-mix-up functions:

  • wp_reset_postdata(): Resets the global $post to the main query's current post (use this after new WP_Query() loops).
  • wp_reset_query(): Restores the main query itself (only needed if you used query_posts()—which you should avoid, by the way).

If you used query_posts() instead of WP_Query, wp_reset_postdata() won't fix the issue—you need wp_reset_query() instead.

Step-by-Step Fixes

  1. Check Your Custom Query Syntax
    If you're using a custom loop, make sure it follows this structure (the reset happens after the loop ends):

    $custom_query = new WP_Query( $your_args );
    
    if ( $custom_query->have_posts() ) {
        while ( $custom_query->have_posts() ) {
            $custom_query->the_post();
            // Your loop content here (the_title(), the_content(), etc.)
        }
        // Reset RIGHT AFTER the loop, not before
        wp_reset_postdata();
    }
    

    If you must use query_posts() (again, not recommended), replace wp_reset_postdata() with wp_reset_query().

  2. Validate $post Before Using Template Functions
    If you're calling functions like get_the_content() outside of a loop, add a check to ensure $post is a valid object first:

    global $post;
    
    if ( is_a( $post, 'WP_Post' ) ) {
        // Safe to call functions that rely on $post
        echo get_the_content();
    } else {
        // Handle the case where $post isn't valid—maybe pass a post ID manually
        echo get_the_content( '', false, 123 ); // Replace 123 with your actual post ID
    }
    
  3. Check for Plugin/Theme Conflicts
    Temporarily switch to a default theme (like Twenty Twenty-Four) and disable all plugins. If the error goes away, re-enable them one by one to find the culprit. Some poorly coded snippets might be modifying the global $post without cleaning up after themselves.

  4. Verify the Post Exists
    If you're working with a specific post ID, double-check that the post actually exists and is published. Passing an invalid ID to functions that expect a WP_Post object can trigger this error too.

Wrap-Up

The key here is making sure the global $post state is clean when template functions run, either by properly resetting custom queries or validating the object before use.

内容的提问来源于stack exchange,提问作者Spencer Phillips

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:10:19