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

开启Debug模式后WordPress taxonomy.php报非对象属性获取通知

Fixing the "Trying to get property of non-object" Notice in WordPress taxonomy.php

Hey there, let's work through fixing that PHP notice you're seeing. The error Trying to get property of non-object on line 3961 of wp-includes/taxonomy.php happens because when the code tries to access $ancestor_term->slug, the $ancestor_term variable isn't a valid term object—it's either false or a WP_Error instead.

Since you confirmed your taxonomy.php is the unmodified WordPress core file, the issue isn't with the core code itself. It's almost always tied to broken term data in your database or a conflict with a plugin/theme interfering with term retrieval. Here are actionable steps to fix it:

  • Check for invalid ancestor term IDs
    The code is fetching ancestor terms for a specific term, but one of those ancestor IDs might point to a term that no longer exists (like a deleted term leftover from a failed import or accidental removal). You can:

    1. Pinpoint which term is triggering the error—look for context in your error_log or visit the taxonomy/term page that causes the notice to appear.
    2. Use a database tool like phpMyAdmin to check the wp_term_taxonomy and wp_terms tables. Verify that all ancestor IDs linked to your problematic term correspond to existing entries in wp_terms. If you find invalid IDs, clean them up manually (make sure to back up your database first!).
  • Add a safety check via a filter (no core edits needed)
    Instead of modifying the core taxonomy.php file, use a WordPress filter to ensure get_term() returns a valid object. Add this code to your active theme's functions.php file (or a custom plugin):

    add_filter('get_term', function($term, $term_id, $taxonomy) {
        // If the term is invalid or an error, return an empty object to avoid notices
        if (!is_object($term) || is_wp_error($term)) {
            return (object) ['slug' => ''];
        }
        return $term;
    }, 10, 3);
    

    This ensures $ancestor_term always has a slug property to access, stopping the notice from triggering.

  • Run WordPress database repair
    WordPress has a built-in tool to fix corrupted database tables. Enable it by adding this line to your wp-config.php file:

    define('WP_ALLOW_REPAIR', true);
    

    Then visit your-site-url/wp-admin/maint/repair.php, run the repair and optimize options. Once done, remove that line from wp-config.php to keep your site secure.

  • Check for plugin/theme conflicts
    Sometimes a third-party plugin or custom theme can disrupt term retrieval. Try:

    1. Disabling all plugins temporarily. If the notice disappears, re-enable them one by one to find the culprit.
    2. Switching to a default WordPress theme (like Twenty Twenty-Four). If the notice goes away, your theme is the issue—reach out to the theme developer for a fix.

The core code assumes get_term() will always return a valid term object, but real-world scenarios like data corruption or plugin interference can break that assumption. The steps above should help you track down and fix the root cause.

内容的提问来源于stack exchange,提问作者Juan Andres

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:19:41