Make WordPress Core

Changeset 60661


Ignore:
Timestamp:
08/26/2025 05:24:06 PM (13 months ago)
Author:
SergeyBiryukov
Message:

Taxonomy: Check the result of get_term() in WP_Term_Query::get_terms().

get_term() can return WP_Error or null on failure, so the result should be verified as a WP_Term instance before accessing the count property.

This commit prevents a PHP warning if get_term() returns null for a child term:

Warning: Attempt to read property "count" on null

Follow-up to [27458], [37572].

Props josephscott, coleatkinson1, kebbet, jakariaistauk, sabernhardt, westonruter, SergeyBiryukov.
Fixes #63877.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-term-query.php

    r59325 r60661  
    850850                                                foreach ( $children as $child_id ) {
    851851                                                        $child = get_term( $child_id, $term->taxonomy );
    852                                                         if ( $child->count ) {
     852
     853                                                        if ( $child instanceof WP_Term && $child->count ) {
    853854                                                                continue 2;
    854855                                                        }
  • trunk/tests/phpunit/tests/term/query.php

    r57750 r60661  
    851851
    852852                $this->assertSameSets( $expected, wp_list_pluck( $found, 'term_id' ) );
     853        }
     854
     855        /**
     856         * Tests that a call to WP_Term_Query::get_terms() does not result in a PHP warning
     857         * when get_term() returns null for a child term.
     858         *
     859         * The warning that we should not see:
     860         * `Warning: Attempt to read property "count" on null`.
     861         *
     862         * @ticket 63877
     863         */
     864        public function test_null_child_term_should_not_throw_warning() {
     865                register_taxonomy(
     866                        'wptests_tax',
     867                        'post',
     868                        array(
     869                                'hierarchical' => true,
     870                        )
     871                );
     872
     873                $t1 = self::factory()->term->create(
     874                        array(
     875                                'taxonomy' => 'wptests_tax',
     876                        )
     877                );
     878
     879                $t2 = self::factory()->term->create(
     880                        array(
     881                                'taxonomy' => 'wptests_tax',
     882                                'parent'   => $t1,
     883                        )
     884                );
     885
     886                $this->term_id = $t2;
     887
     888                add_filter( 'get_term', array( $this, 'filter_term_to_null' ) );
     889                $q = new WP_Term_Query(
     890                        array(
     891                                'taxonomy'   => 'wptests_tax',
     892                                'hide_empty' => true,
     893                                'fields'     => 'ids',
     894                        )
     895                );
     896                remove_filter( 'get_term', array( $this, 'filter_term_to_null' ) );
     897
     898                $this->assertIsArray( $q->terms, 'The result should be an array.' );
     899                $this->assertEmpty( $q->terms, 'The result should be empty.' );
    853900        }
    854901
Note: See TracChangeset for help on using the changeset viewer.