Make WordPress Core

Opened 8 months ago

Last modified 8 months ago

#59785 new defect (bug)

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

Reported by: faar's profile Faar Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.3.3
Component: Taxonomy Keywords:
Focuses: Cc:

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 8 weeks ago.
Hello, I can confirm this solved a problem we had on a multisite instance. Thanks!

Download all attachments as: .zip

Change History (2)

#1 @Faar
8 months 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.

@gliautard
8 weeks ago

Hello, I can confirm this solved a problem we had on a multisite instance. Thanks!

Note: See TracTickets for help on using tickets.