ClassicPress PetitionsClassicPress Petitions
This is a read-only archive. Post or comment on the live version of this page on our forums.

Fix WP_Query post__in issue with empty array input

January 7, 2019 · 02:55 · Lance Willis
Description

Ticket #28099 <https://core.trac.wordpress.org/ticket/28099> Passing an empty array to post__in will return have_posts() as true (and all posts will be returned).

If the post__in array is empty, the query should return no results instead of all posts. Currently to get the expected query result, the post__in argument must be checked for emptiness and the first element of the array set to 0 before passing to WP_Query->set().

Voters
Discussion
Lance Willis

WP fixed this years ago, but it broke so many plugins they reverted back to the original code. CP2 would be a good opportunity to clear out of this legacy issue.

Tim Kaye

@Lance Willis: I took a look at the ticket to which you refer, and it seems to me that the reversion was really a case of the tail wagging the dog. Because some plugin developers were "doing it wrong," this change caused those plugins to break. But even some of those developers themselves said on that ticket that they could easily modify their code and that this change was a good idea.

So I vote to do this. It would be a breaking change but a sensible one. It seems bizarre that passing an empty array returns all posts!

Lance Willis

The workaround I described above will still work after the issue is fixed. Code that expects the current behavior should not set post__in if the array is empty. These workarounds could be added to the CP2 release notes for devs that want their code to work in CP1, CP2, and WP.