Make WordPress Core


Ignore:
Timestamp:
09/05/2026 01:39:01 AM (2 months ago)
Author:
westonruter
Message:

Taxonomy: Fix wp_get_object_terms() when requesting a count.

Requesting a count passed the numeric string get_terms() returns straight to array_merge(), which warns and yields null on PHP 7.4 and throws a TypeError on PHP 8.0 and later. The count is now cast before it is merged, and summed once every taxonomy has been queried, so the function returns the numeric string its documentation has described since r49947. An empty object or taxonomy list returns '0' for the same reason, rather than the empty array that contradicted the documented type.

A taxonomy registered with an args array is queried by a separate recursive call, and those results were merged with array_merge() unconditionally. For the id=> values of fields the term IDs are integer array keys, which array_merge() renumbers, so such a taxonomy came back keyed from zero. The recursive branch now uses the union operator for those, as the merge for the remaining taxonomies has done since r41809.

The functions get_terms(), wp_get_object_terms(), wp_count_terms(), wp_get_post_categories(), wp_get_post_tags() and wp_get_post_terms() each gain a conditional return type that resolves the result from the fields value, falling back to what that function's own default answers with, as was done for WP_Term_Query::query() in r63358.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/7278.
Follow-up to r38667, r40513, r41809, r49947, r63358.

Props marian1, westonruter, swissspidy.
See #65817, #66049.
Fixes #61936.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term.php

    r56971 r63488  
    7878                // There are 5 posts, all Uncategorized.
    7979                $this->assertSame( '1', $count );
     80        }
     81
     82        /**
     83         * Ensures a queried parent term outside the taxonomy hierarchy counts as the integer 0.
     84         *
     85         * Every other outcome is a numeric string, so this is the one case where the return
     86         * type is not a string at all.
     87         *
     88         * @ticket 61936
     89         *
     90         * @covers ::wp_count_terms
     91         */
     92        public function test_wp_count_terms_should_return_integer_zero_for_a_parent_outside_the_hierarchy() {
     93                register_taxonomy( 'wptests_tax_hierarchical', 'post', array( 'hierarchical' => true ) );
     94
     95                $term_id = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_hierarchical' ) );
     96                $this->assertIsInt( $term_id, 'The term was not created.' );
     97
     98                $count = wp_count_terms(
     99                        array(
     100                                'taxonomy' => 'wptests_tax_hierarchical',
     101                                'parent'   => 99999,
     102                        )
     103                );
     104                $this->assertSame( 0, $count, 'Incorrect count for a parent outside the hierarchy.' );
     105
     106                $count = wp_count_terms(
     107                        array(
     108                                'taxonomy' => 'wptests_tax_hierarchical',
     109                                'child_of' => 99999,
     110                        )
     111                );
     112                $this->assertSame( 0, $count, 'Incorrect count for a child_of outside the hierarchy.' );
    80113        }
    81114
Note: See TracChangeset for help on using the changeset viewer.