Forked from jasonbahl/order-by-acf-like-count.php
Created
August 12, 2019 08:10
Star
You must be signed in to star a gist
Shows how to add a custom order value to a connection to order by a custom field.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // open PHP | |
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_key'] = 'like_count'; | |
$query_args['orderby'] = 'meta_value_num'; | |
$query_args['order'] = $orderby['order']; | |
} | |
} | |
return $query_args; | |
}, 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment