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/src/wp-includes/post.php

    r63442 r63488  
    44534453 * @param array $args    Optional. Category query parameters. Default empty array.
    44544454 *                       See WP_Term_Query::__construct() for supported arguments.
    4455  * @return array|WP_Error List of categories. If the `$fields` argument passed via `$args` is 'all' or
    4456  *                        'all_with_object_id', an array of WP_Term objects will be returned. If `$fields`
    4457  *                        is 'ids', an array of category IDs. If `$fields` is 'names', an array of category names.
    4458  *                        WP_Error object if 'category' taxonomy doesn't exist.
     4455 * @return int[]|WP_Term[]|string[]|string|WP_Error List of categories. An array of category IDs by
     4456 *                                                  default, and if the `$fields` argument passed via
     4457 *                                                  `$args` is 'ids'. If `$fields` is 'all' or
     4458 *                                                  'all_with_object_id', an array of WP_Term objects.
     4459 *                                                  If `$fields` is 'names', an array of category names.
     4460 *                                                  If `$fields` is 'count', a count thereof as a numeric
     4461 *                                                  string. WP_Error object if 'category' taxonomy
     4462 *                                                  doesn't exist.
     4463 *
     4464 * @phpstan-return (
     4465 *     $args is array{ fields: 'count', ... }
     4466 *         ? numeric-string|WP_Error
     4467 *         : ( $args is array{ fields: 'id=>parent', ... }
     4468 *             ? array<int, int>|WP_Error
     4469 *             : ( $args is array{ fields: 'names'|'slugs', ... }
     4470 *                 ? string[]|WP_Error
     4471 *                 : ( $args is array{ fields: 'id=>name'|'id=>slug', ... }
     4472 *                     ? array<int, string>|WP_Error
     4473 *                     : ( $args is array{ fields: 'all'|'all_with_object_id', ... }
     4474 *                         ? WP_Term[]|WP_Error
     4475 *                         : int[]|WP_Error ) ) ) )
     4476 * )
    44594477 */
    44604478function wp_get_post_categories( $post_id = 0, $args = array() ) {
    … …  
    44814499 * @param array $args    Optional. Tag query parameters. Default empty array.
    44824500 *                       See WP_Term_Query::__construct() for supported arguments.
    4483  * @return array|WP_Error Array of WP_Term objects on success or empty array if no tags were found.
    4484  *                        WP_Error object if 'post_tag' taxonomy doesn't exist.
     4501 * @return WP_Term[]|int[]|string[]|string|WP_Error Array of WP_Term objects on success or empty array if no
     4502 *                                                  tags were found. A count thereof as a numeric string if
     4503 *                                                  the `$fields` argument passed via `$args` is 'count'.
     4504 *                                                  WP_Error object if 'post_tag' taxonomy doesn't exist.
     4505 *
     4506 * @phpstan-return (
     4507 *     $args is array{ fields: 'count', ... }
     4508 *         ? numeric-string|WP_Error
     4509 *         : ( $args is array{ fields: 'ids'|'tt_ids', ... }
     4510 *             ? int[]|WP_Error
     4511 *             : ( $args is array{ fields: 'id=>parent', ... }
     4512 *                 ? array<int, int>|WP_Error
     4513 *                 : ( $args is array{ fields: 'names'|'slugs', ... }
     4514 *                     ? string[]|WP_Error
     4515 *                     : ( $args is array{ fields: 'id=>name'|'id=>slug', ... }
     4516 *                         ? array<int, string>|WP_Error
     4517 *                         : WP_Term[]|WP_Error ) ) ) )
     4518 * )
    44854519 */
    44864520function wp_get_post_tags( $post_id = 0, $args = array() ) {
    … …  
    45024536 *     @type string $fields Term fields to retrieve. Default 'all'.
    45034537 * }
    4504  * @return array|WP_Error Array of WP_Term objects on success or empty array if no terms were found.
    4505  *                        WP_Error object if `$taxonomy` doesn't exist.
     4538 * @return WP_Term[]|int[]|string[]|string|WP_Error Array of WP_Term objects on success or empty array if no
     4539 *                                                  terms were found. A count thereof as a numeric string if
     4540 *                                                  the `$fields` argument passed via `$args` is 'count'.
     4541 *                                                  WP_Error object if `$taxonomy` doesn't exist.
     4542 *
     4543 * @phpstan-return (
     4544 *     $args is array{ fields: 'count', ... }
     4545 *         ? numeric-string|WP_Error
     4546 *         : ( $args is array{ fields: 'ids'|'tt_ids', ... }
     4547 *             ? int[]|WP_Error
     4548 *             : ( $args is array{ fields: 'id=>parent', ... }
     4549 *                 ? array<int, int>|WP_Error
     4550 *                 : ( $args is array{ fields: 'names'|'slugs', ... }
     4551 *                     ? string[]|WP_Error
     4552 *                     : ( $args is array{ fields: 'id=>name'|'id=>slug', ... }
     4553 *                         ? array<int, string>|WP_Error
     4554 *                         : WP_Term[]|WP_Error ) ) ) )
     4555 * )
    45064556 */
    45074557function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) {
Note: See TracChangeset for help on using the changeset viewer.