Post Types Order hooks posts_orderby at priority 99 and rewrites the ORDER BY
SQL directly, which breaks WPGraphQL's cursor pagination. This drops that filter
for GraphQL requests and expresses the manual order through WP_Query args
instead, where WPGraphQL can see it and paginate correctly.
This replaces an earlier version of this snippet from 2022. That one works for the case it was written for, but it has three problems that took a while to track down on a real site.
1. It registers at load time, not inside pre_get_posts.
WPGraphQL applies graphql_post_object_connection_query_args while it is
building the query args, which happens before pre_get_posts fires for that
query. So registering the filter from inside pre_get_posts misses the very
query it is meant to fix. It also re-registers a new closure on every subsequent
WP_Query in the request.
This one is genuinely hard to see, because it can appear to work. Anything that
runs an extra WP_Query earlier in the request (WPGraphQL Smart Cache looking up
a persisted query document, for example) fires pre_get_posts while
is_graphql_request() is already true, which registers the filter in time for
the connection that follows. Add or remove a plugin and the behavior changes
with no apparent cause.
2. It no longer applies manual order to the post type.
Manual drag-and-drop order is only meaningful for types that are actually
curated that way. Posts almost never are, so every row sits at menu_order 0.
Ordering by an all-zero column collapses the blog onto WPGraphQL's cursor
stability tiebreaker instead of date, which is how a backdated post can end up
leading the blog for no visible reason.
Filter or edit the in_array( 'post', ... ) check if you do curate posts
manually.
3. It builds the orderby array correctly.
WP_Query reads the key as the field name in an orderby array, so
$orderby[] = 'date' produces a numerically keyed entry that WP_Query silently
drops. The old version did exactly that when carrying over an existing string
orderby, so the date sort disappeared. This normalizes whatever is already set
into [ field => direction ] form first, and adds date as a final tiebreaker
so an all-zero menu_order still sorts sensibly.
Drop it in an mu-plugin, or paste it into a snippets plugin. If you use Code
Snippets, omit the opening <?php tag, since it eval()s the code directly.