Make WordPress Core

#63920 closed defect (bug) (fixed)

get_adjacent_post() has a misplaced check for WP_Error

Reported by: kitchin Owned by: westonruter
Priority: normal Milestone: 6.9
Component: Posts, Post Types Version: 4.0
Severity: normal Keywords: has-patch has-unit-tests commit
Cc: Focuses:

Description

The function calls is_wp_error( $term_array ) after $term_array = array_map( 'intval', $term_array ).

Attachments (1)

63920.patch (851 bytes ) - added by kitchin 12 months ago.

Download all attachments as: .zip

Change History (13)

@kitchin
12 months ago

#1 follow-up: @westonruter
12 months ago

  • Keywords has-patch added
  • Milestone Awaiting Review6.9

@kitchin thanks for the report and the patch. It looks good to me. Would you like to add a test case for the scenario that causes a failure without the fix?

Also, it would be good to open a pull request with your patch so we can see the existing automated tests run.

#2 in reply to: ↑ 1 @SirLouen
12 months ago

  • Keywords needs-testing needs-unit-tests added
  • Version trunk3.7

This was introduced in one of those megalodon commits #26937/[27836], so not rare to see this. I wonder if PHPStan would have spotted it.

Replying to westonruter:

Also, it would be good to open a pull request with your patch so we can see the existing automated tests run.

Tests are passing, but just because there are no unit tests covering this. It would be great to add them.

Moreover, this can be a great opportunity to reunify all the hanging get_adjacent_post tests in other files that don't belong.

Let me add the needs-unit-tests tag and if I find some time I will work on this @kitchin if you want to do it, send a PR as @westonruter commented.

#3 follow-up: @SergeyBiryukov
12 months ago

  • Version 3.74.0

Introduced in [29248] / #22112.

#4 in reply to: ↑ 3 @SirLouen
12 months ago

Replying to SergeyBiryukov:

Introduced in [29248] / #22112.

True, in origin wp_get_object_terms could return WP_Error it was in [29248] with array_map in between when it broke this conditional.

#5 @pmbaldha
12 months ago

@kitchin Thanks for reporting the issue and creating the patch for it. Can you please give us a test case so we can test it? You can refer to https://make.wordpress.org/test/2025/05/15/building-the-testing-use-case/.

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


12 months ago
#6

Added the is_wp_error check before calling other array functions so that no warning is thrown.

Trac ticket: https://core.trac.wordpress.org/ticket/63920

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


12 months ago
#7

  • Keywords has-unit-tests added; needs-unit-tests removed

Improves error handling in get_adjacent_post to avoid PHP errors when passing non-array values to array functions. Adds phpunit tests to confirm behavior.

Trac ticket: https://core.trac.wordpress.org/ticket/63920

#8 @mindctrl
12 months ago

Hi all, I've added a PR that matches the original patch, but with some suggested phpunit test cases.

@westonruter commented on PR #9861:


12 months ago
#9

Hello! I'm curious about your PR here, and if you factored in the existing patch from @kitchin as proposed on the ticket?

That patch has been iterated on by @mindctrl with added unit tests in https://github.com/WordPress/wordpress-develop/pull/9863 so it's likely to be what gets committed.

@mindctrl commented on PR #9863:


12 months ago
#10

@westonruter thank you for the review. I've applied your suggestions.

#11 @westonruter
12 months ago

  • Keywords commit added; needs-testing removed
  • Owner set to westonruter
  • Status newaccepted

#12 @westonruter
12 months ago

  • Resolutionfixed
  • Status acceptedclosed

In 60733:

Posts, Post Types: Fix placement of WP_Error check in get_adjacent_post().

Props kitchin, mindctrl, westonruter, SirLouen, SergeyBiryukov, pmbaldha.
Fixes #63920.

Note: See TracTickets for help on using tickets.