Skip to content

Instantly share code, notes, and snippets.

@shishirraven
Created January 24, 2022 01:31
Show Gist options
  • Save shishirraven/651aae2a8b227dc91458f0053f41c255 to your computer and use it in GitHub Desktop.
Save shishirraven/651aae2a8b227dc91458f0053f41c255 to your computer and use it in GitHub Desktop.
Adding Orderby support for custom meta fields
add_filter(
'rest_' . $post_type . '_query',
function ( $args, $request ) {
$fields = ["first_name","last_name","address","email"];
$order_by = $request->get_param( 'orderby' );
if ( isset( $order_by ) && in_array($order_by, $fields)) {
$args['meta_key'] = $order_by;
$args['orderby'] = 'meta_value'; // user 'meta_value_num' for numerical fields
}
return $args;
},
10,
2
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment