#65915 closed defect (bug) (duplicate)
Fatal error in WP_Term::get_instance() when the terms cache holds a non-object
| Reported by: | BogdanUngureanu | Owned by: | westonruter |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Taxonomy | Version: | trunk |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
WP_Term::get_instance() only checks that the cached value is truthy before using it:
$_term = wp_cache_get( $term_id, 'terms' ); if ( ! $_term || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) {
If the terms cache group returns something truthy that isn't an object — an array, an integer, a string the guard passes, the database lookup is skipped, and the value is handed to new WP_Term( $_term ), which calls get_object_vars() on it:
TypeError: get_object_vars(): Argument #1 ($object) must be of type object, array given
This is reachable through get_terms(). On a cached query, WP_Term_Query::get_terms() calls populate_terms(), which calls get_term() for each ID, which calls WP_Term::get_instance() — so a single bad entry in the terms group fatals the request instead of falling back to the database.
Core always caches term objects via update_term_cache(), but a persistent object cache can return something else: a failed unserialize, a key collision, or a drop-in or plugin writing to the group directly.
Patch: also require the cached value to be an object, so a bad entry falls through to the existing database lookup rather than fataling.
if ( ! is_object( $_term ) || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) {
is_object already checks for the falsy values. I'll open a PR in the GitHub mirror with the fix and with a unit test for it.
Change History (9)
This ticket was mentioned in PR #13204 on WordPress/wordpress-develop by @BogdanUngureanu.
3 months ago
#1
- Keywords has-patch has-unit-tests added
@BogdanUngureanu commented on PR #13204:
3 months ago
#2
Discovered as an issue in WooCommerce: https://github.com/woocommerce/woocommerce/issues/67520
@BogdanUngureanu commented on PR #13204:
3 months ago
#3
Another thing to note here: with PHP 8, get_object_vars now triggers a TypeError. Previously (PHP 7 and older) it was triggering a PHP warning. This means that on older versions of PHP, this introduced a silent bug that returned a blank WP_Term object (without even an ID).
This can be observed here: https://3v4l.org/8f1VN#v
@BogdanUngureanu commented on PR #13204:
3 months ago
#6
Thanks @westonruter for the fast response. I've pushed a commit that updates the test.
Let me know if more changes are needed.
@BogdanUngureanu commented on PR #13204:
3 months ago
#7
This can be closed since the change is the same as the one from https://github.com/WordPress/wordpress-develop/pull/13270
@westonruter commented on PR #13204:
3 months ago
#9
@BogdanUngureanu Sorry, I failed to remember this PR when I was working on the other. I've added you to the props list for r63354 in the Core Props, so you'll be credited in the release.
A truthy non-object in the
termscache group skipped the database lookup and fataled inget_object_vars(), taking down anyget_terms()call that reached it.This happens when the site uses a persistent cache (like redis) that fails to unserialize the object. In which case, it returns a string.
In this case, Core should check if the value returned from the cache can be properly used. If not, it should pull the term directly from the database.
Trac ticket: https://core.trac.wordpress.org/ticket/65915#ticket
## Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Opus 4.8
Used for: Writing the test