Make WordPress Core

Opened 3 years ago

Last modified 3 weeks ago

#59785 new defect (bug)

Fatal error: Attempt to assign property “object_id” on bool in class-wp-term-query.php

Reported by: Faar Owned by:
Priority: normal Milestone: Awaiting Review
Component: Taxonomy Version: 6.3.3
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

The populate_terms() function in this class still throws a PHP fatal error on the get_term() query. Why?
In special circumstances, get_term() returns NULL and the variable $term is not checked to see if it is still an object or already boolean.
$term = NULL and this leads to a fatal PHP error and the page loading is aborted because $term->object_id does not work with NULL.
NULL->object_id

More to read here:
​https://wordpress.org/support/topic/fatal-error-attempt-to-assign-property-object_id-on-bool/

Attachments (1)

59785-fix-fatal-error-attempt-to-assign-property-object_id-on-bool.diff​ (682 bytes ) - added by gliautard 3 years ago.
Hello, I can confirm this solved a problem we had on a multisite instance. Thanks!

Download all attachments as: .zip

Change History (8)

#1 @Faar
3 years ago

This solved the Error:

<?php
        protected function populate_terms( $terms ) {
                $term_objects = array();
                if ( ! is_array( $terms ) ) {
                        return $term_objects;
                }

                foreach ( $terms as $key => $term_data ) {
                        if ( is_object( $term_data ) && property_exists( $term_data, 'term_id' ) ) {
                                $term = get_term( $term_data->term_id );
                                if ( is_object( $term ) && property_exists( $term_data, 'object_id' ) ) { // modfied 2023_10_26 Faar: is_object()
               $term->object_id = (int) $term_data->object_id;
                                }
                                if ( property_exists( $term_data, 'count' ) ) {
                                        $term->count = (int) $term_data->count;
                                }
                        } else {
                                $term = get_term( $term_data );
                        }

                        if ( $term instanceof WP_Term ) {
                                $term_objects[ $key ] = $term;
                        }
                }

                return $term_objects;
        }

Asking, if is object $term.

#2 @robinmartijn
2 years ago

I can also confirm that it fixed the issue for us as well (in 6.6.2). What do we need to do to get this into core ASAP?

This ticket was mentioned in ​Slack in #core by robinmartijn. ​View the logs.


2 years ago

#4 @liamsputnik
17 months ago

Is there any indication when this fix is going to be merged?
It seems its not had any activity for a while.

This ticket was mentioned in ​PR #14843 on ​WordPress/wordpress-develop by ​@nofearinc.


3 weeks ago
#5

  • Keywords has-patch has-unit-tests added

In WP_Term_Query::populate_terms(), rows that carry an object_id or count are assigned to the result of get_term() before it is checked. When the term no longer resolves, for example because a persistent cache still lists a deleted term or a get_term filter returns null, this is a fatal error: Attempt to assign property "object_id" on null.

The function already discards anything that is not a WP_Term a few lines later, so this skips such terms before the assignments, the same guard [60661] added for child terms in #63877. One guard covers both the object_id and the count assignment.

Testing

  • vendor/bin/phpunit --filter test_unresolvable_term_with_object_id_should_be_discarded fails without the patch with Attempt to assign property "object_id" on null and passes with it.
  • vendor/bin/phpunit --group taxonomy (1122 tests) passes.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: reproducing the fatal in trunk, drafting the fix and test, and running them. I reviewed every line.

---
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See ​GitHub Pull Requests for Code Review in the Core Handbook for more details.

🤖 Generated with ​Claude Code

#6 @nofearinc
3 weeks ago

Still reproducible in trunk. ​https://github.com/WordPress/wordpress-develop/pull/14843 skips terms that no longer resolve before object_id and count are assigned, the same guard [60661] added for child terms in #63877, and adds a unit test. The change in comment:1 only guards object_id, so the count assignment right after it could still fail. Thanks @Faar and @gliautard for the earlier fixes.

AI assistance: Claude Code (Claude Opus 5.5) for reproducing the fatal and drafting the fix and test; reviewed and tested by me.

#7 @Faar
3 weeks ago

Thanks @nofearinc
I hit this on a multisite network with about 30 blogs. A language plugin had not migrated its old tables correctly, and some columns (with data) were missing but i ddin'tknow which one. After blog owners changed their language settings false through the normal admin screen years before, get_term() returned null inside WP_Term_Query::populate_terms().

The code assigns $term->object_id and $term->count without checking that $term is a WP_Term, which causes a fatal error on PHP 8. Note that a guard for object_id alone is not enough, since the count assignment fails the same way. Because this fatal-error i lost my job for the multisite, it isn't WordPress anymore.

Note: See TracTickets for help on using tickets.