Make WordPress Core

#64247 closed defect (bug) (fixed)

REST API: `class_list` sometimes returned as an object

Reported by: dlh Owned by: westonruter
Priority: normal Milestone: 7.0
Component: REST API Version: 6.6
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

The class_list property in the REST API posts endpoint returns the array of classes generated by get_post_class(). The final step in get_post_class() is to run array_unique() over the array, which removes duplicate classes but doesn't reset the array keys, usually causing the keys to become non-sequential, and in turn causing the array to be converted to a JSON object in the REST response. See the attached screenshot showing an API response from a site affected by this issue.

(A similar problem was fixed just the other day in [61210].)

The linked PR would address the issue within the controller, since the array needs to be indexed at that point no matter how the list of classes is generated.

Attachments (1)

Screenshot 2025-11-12 at 11.58.36 AM.png (422.5 KB ) - added by dlh 10 months ago.

Download all attachments as: .zip

Change History (6)

This ticket was mentioned in PR #10515 on WordPress/wordpress-develop by @dlh.


10 months ago
#1

  • Keywords has-patch has-unit-tests added

@Mamaduka commented on PR #10515:


10 months ago
#2

The fix makes sense.

The unit tests are failing with following error:

Parse error: syntax error, unexpected ')' in /var/www/tests/phpunit/tests/rest-api/rest-posts-controller.php on line 2795

@dlh commented on PR #10515:


10 months ago
#3

Fixed!

#4 @westonruter
10 months ago

  • Milestone Awaiting Review7.0
  • Owner set to westonruter
  • Status newreviewing

#5 @westonruter
10 months ago

  • Resolutionfixed
  • Status reviewingclosed

In 61269:

Posts, Post Types: Ensure get_post_class() returns a list.

This avoids the REST API erroneously returning an object in the class_list property returned by WP_REST_Posts_Controller::prepare_item_for_response().

Developed in https://github.com/WordPress/wordpress-develop/pull/10515

Props dlh, mamaduka, westonruter.
Fixes #64247.

Note: See TracTickets for help on using tickets.