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/phpstan/baselines/property.notFound.neon

    r63367 r63488  
    2020        ignoreErrors:
    2121                -
     22                        message: '#^Access to an undefined property WP_Term\:\:\$id\.$#'
     23                        identifier: property.notFound
     24                        count: 1
     25                        path: ../../../src/wp-admin/includes/ajax-actions.php
     26                -
     27                        message: '#^Access to an undefined property WP_Term\:\:\$link\.$#'
     28                        identifier: property.notFound
     29                        count: 1
     30                        path: ../../../src/wp-admin/includes/ajax-actions.php
     31                -
    2232                        message: '#^Access to an undefined property WP_Upgrader_Skin\:\:\$language_update\.$#'
    2333                        identifier: property.notFound
Note: See TracChangeset for help on using the changeset viewer.