-
-
Save jasonbahl/da87dbccb58f1323a324a9b3e8952d6c to your computer and use it in GitHub Desktop.
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); |
@juanu96 I believe if the field is storing numbers, you need:
$query_args['orderby'] = 'meta_value_num';
instead of:
$query_args['orderby'] = 'meta_value';
@juanu96 I believe if the field is storing numbers, you need:
$query_args['orderby'] = 'meta_value_num';
instead of:
$query_args['orderby'] = 'meta_value';
yes, this worked for me, thank you very much for your help
Hi all,
I've used this solution in my own code.
add_filter( 'graphql_PostObjectsConnectionOrderbyEnum_values', function( $values ) {
$values['PARTNER_TIER'] = [
'value' => 'partner_tier',
'description' => __( 'Tier Level of the partner'),
];
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'] ) || 'partner_tier' !== $orderby['field'] ) {
continue;
}
$query_args['meta_type'] = 'NUMERIC';
$query_args['meta_key'] = 'partner_tier';
$query_args['orderby']['meta_value_num'] = $orderby['order'];
}
}
return $query_args;
}, 10, 3);
And while the initial call works, the cursor pagination is not working, and returning and empty set of partners when calling the next page.
Here's the working initial query.
And here's the query calling the next page
Was wondering if anyone else here had a similar issue or if there's something I am doing incorrectly
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.
@mickras I never ended up finding a solution, so I had moved back to the Wordpress REST API and created my own search filters
Thanks for posting this example, @jasonbahl ! It worked flawlessly for my use case (ordering posts by a Number ACF field VIEW_COUNT
).
Hi, this answer is amazing, but it only works for the first element when it comes to numbers and sorts only the first numeric value, the rest of the digits are ignored.
I used it and I ordered the prices in this way