Skip to content

Instantly share code, notes, and snippets.

@jasonbahl
Last active June 13, 2023 16:29
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jasonbahl/da87dbccb58f1323a324a9b3e8952d6c to your computer and use it in GitHub Desktop.
Save jasonbahl/da87dbccb58f1323a324a9b3e8952d6c to your computer and use it in GitHub Desktop.
Shows how to add a custom order value to a connection to order by a custom field.
add_filter( 'graphql_PostObjectsConnectionOrderbyEnum_values', function( $values ) {
$values['LIKE_COUNT'] = [
'value' => 'like_count',
'description' => __( 'The number of likes on the post', 'wp-graphql' ),
];
return $values;
} );
add_filter( 'graphql_post_object_connection_query_args', function( $query_args, $source, $input ) {
if ( isset( $input['where']['orderby'] ) && is_array( $input['where']['orderby'] ) ) {
foreach( $input['where']['orderby'] as $orderby ) {
if ( ! isset( $orderby['field'] ) || 'like_count' !== $orderby['field'] ) {
continue;
}
$query_args['meta_type'] = 'NUMERIC';
$query_args['meta_key'] = 'like_count';
$query_args['orderby']['meta_value_num'] = $orderby['order'];
}
}
return $query_args;
}, 10, 3);
@mickras
Copy link

mickras commented Jun 13, 2023

Was wondering if anyone else here had a similar issue or if there's something I am doing incorrectly

@ndigenpcc Did you ever find a solution to your problem? I'm facing something similar, and can't seem to find the bug.

@ndigenpcc
Copy link

@mickras I never ended up finding a solution, so I had moved back to the Wordpress REST API and created my own search filters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment