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.nonObject.neon

    r63383 r63488  
    2525                        path: ../../../src/wp-admin/includes/ajax-actions.php
    2626                -
    27                         message: '#^Cannot access property \$id on int\|string\|WP_Term\.$#'
    28                         identifier: property.nonObject
    29                         count: 1
    30                         path: ../../../src/wp-admin/includes/ajax-actions.php
    31                 -
    32                         message: '#^Cannot access property \$link on int\|string\|WP_Term\.$#'
    33                         identifier: property.nonObject
    34                         count: 1
    35                         path: ../../../src/wp-admin/includes/ajax-actions.php
    36                 -
    3727                        message: '#^Cannot access property \$name on array\|object\.$#'
    3828                        identifier: property.nonObject
Note: See TracChangeset for help on using the changeset viewer.