-
Hey team,
I am working on adding cursor-based pagination support when querying for redirections in WPGraphQL for Rank Math SEO.
I believe the correct way to do this is with the
rank_math/redirection/get_redirections_query
action, however calling$table->where()
overrides the existing sql rules, instead of appending it.Is this a bug in the
Query_Builder
implementation, or am I doing something wrong (and if so, do you have a better approach)?(PS: my assumption is that we will face the same issue when trying to expose 404 Monitor logs to WPGraphQL, so solving this is essential to Rank Math feature parity with headless).
Here’s a simplified code example to illustrate the issue:
add_action(
'rank_math/redirection/get_redirections_query',
function( $table, $args ) {
// 'after' is the redirection ID that we're paginating from.
if ( empty( $args['after'] ) ) {
return;
}// Confirm $table->sql_clauses has the correct
where
,limit
, `orderby, etc.
error_log( print_r( $table, true ) );// Get the operator from the args.
$operator = 'DESC' === $args['order'] ? '<' : '>';// Get the value from the redirection.
$cursor = \RankMath\Redirections\DB::get_redirection_by_id( absint( $args['after'] ) );
$value = $cursor[ $args['orderby'] ];// I want to *add* the where clause to the existing ones.
$table->where( $args['orderby'], $operator, $value ); // e.g. 'id', '<', 5// $table->sql_clauses has been OVERRIDDEN, so only 'WHERE id < '5' exists.
error_log( print_r($table, true );
},
10,
2
);Pastebin of the logged table before/after: https://pastebin.com/VTcXQ9Wm
The ticket ‘Adding WHERE clause to redirection query overwrites existing query’ is closed to new replies.