Skip to content

Instantly share code, notes, and snippets.

@jasonbahl
Created September 24, 2026 20:05
Show Gist options
  • Select an option

  • Save jasonbahl/c5eb742003a5ee2ef854f5054f19d1ca to your computer and use it in GitHub Desktop.

Select an option

Save jasonbahl/c5eb742003a5ee2ef854f5054f19d1ca to your computer and use it in GitHub Desktop.
Post Types Order + WPGraphQL ordering bridge, fixes filter registration timing and orderby array handling

Post Types Order + WPGraphQL ordering bridge

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.

What changed

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.

Installing

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.

<?php
/**
* Post Types Order + WPGraphQL ordering bridge.
*
* Post Types Order hooks `posts_orderby` at priority 99 and rewrites the ORDER BY
* SQL directly, which breaks WPGraphQL's cursor pagination. We drop that filter for
* GraphQL requests and express the manual order through WP_Query args instead.
*/
// Registered at load time, NOT inside pre_get_posts: WPGraphQL applies this filter
// while it builds the query args, which happens BEFORE pre_get_posts fires for that
// query. Registering it from pre_get_posts misses the very query it means to fix,
// and re-registers a new closure on every subsequent WP_Query in the request.
add_filter(
'graphql_post_object_connection_query_args',
function ( $args, $source, $input_args, $context, $info ) {
global $CPTO;
if ( ! $CPTO ) {
return $args;
}
// Post Types Order adds this on every query, so remove it here, where we
// know a GraphQL connection query is about to run.
remove_filter( 'posts_orderby', [ $CPTO, 'posts_orderby' ], 99 );
// An explicit orderby from the GraphQL query always wins.
if ( ! empty( $input_args['where']['orderby'] ) ) {
return $args;
}
// Chronological content is ordered by date. Manual drag-and-drop order is
// only meaningful for the types that are actually curated that way, and
// applying it to posts leaves every row at menu_order 0, which collapses
// the blog onto the cursor tiebreaker instead of date.
$post_types = array_filter( (array) ( $args['post_type'] ?? [] ) );
if ( in_array( 'post', $post_types, true ) ) {
return $args;
}
$direction = ! empty( $input_args['last'] ) ? 'DESC' : 'ASC';
// Normalize whatever is already set into WP_Query's [ field => direction ]
// form. WP_Query reads the KEY as the field name and silently skips
// numerically-keyed entries, so `$orderby[] = 'date'` loses the date sort.
$orderby = [];
if ( ! empty( $args['orderby'] ) ) {
$existing = is_string( $args['orderby'] )
? preg_split( '/[\s,]+/', $args['orderby'], -1, PREG_SPLIT_NO_EMPTY )
: $args['orderby'];
foreach ( (array) $existing as $key => $value ) {
if ( is_int( $key ) ) {
$orderby[ $value ] = $args['order'] ?? $direction;
} else {
$orderby[ $key ] = $value;
}
}
}
// Manual order first, then whatever was already there, with date as the
// final tiebreaker so an all-zero menu_order still sorts sensibly.
$orderby = array_merge( [ 'menu_order' => $direction ], $orderby );
if ( ! isset( $orderby['date'] ) ) {
$orderby['date'] = $direction;
}
$args['orderby'] = $orderby;
return $args;
},
10,
5
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment