Fix pagination conflict with the “Advanced Taxonomy Terms Order” plugin

When using the Advanced Taxonomy Terms Order plugin along with WPGraphQL, you might experience some issues with paginated queries.

This snippet should help correct the conflict:

add_action( 'pre_get_terms', function() {

	// if WPGraphQL isn't active or CPTUI isn't active, carry on and bail early
	if ( ! function_exists( 'is_graphql_request' ) || ! is_graphql_request() || ! function_exists( 'TO_activated' ) ) {

        // Remove the terms_clauses filter from the Term Order plugin
        // as this filter runs after WPGraphQL has determined how
        // to resolve paginated requests with order arguments applied
	remove_filter( 'terms_clauses', 'TO_apply_order_filter', 2 );

        // Set the query order and orderby to term_order
        // to match the intent of the Term Order plugin
	add_filter( 'graphql_term_object_connection_query_args', function( $query_args, $source, $input_args, $context, $info ) {

		$query_args['orderby'] = ! empty( $input_args['orderby'] ) ? $input_args['orderby'] : 'term_order';
		$query_args['order'] = ! empty( $input_args['order'] ) ? $input_args['order'] : ( ! empty( $input_args['last'] ) ? 'DESC' : 'ASC' );

		return $query_args;

	}, 10, 5 );


What this snippet does:

First, this hooks into pre_get_terms which fires when WP_Term_Query is executing to get Terms out of the database.

This checks to see if the request is a GraphQL request and whether Term Order plugin is active. If these conditions are not met, nothing happens.

If these conditions are met, we carry on.

Next, we remove the “terms_clauses” filter from the Terms Order plugin, as it modifies the SQL statement directly, after WPGraphQL has determined how to map order args to SQL.

Then, we add our own filter back to WPGraphQLs Term Object Connection query args and set the order and orderby arguments here. This ensures that the order will be properly mapped to WPGraphQLs cursor pagination logic.

Published by Jason Bahl

Jason is a Principal Software Engineer at WP Engine based in Denver, CO where he maintains WPGraphQL. When he's not writing code or evangelizing about GraphQL to the world, he enjoys escaping from escape rooms, playing soccer, board games and Fortnite.