Skip to content

Instantly share code, notes, and snippets.

@jvandijk
Created November 3, 2015 13:27
Show Gist options
  • Save jvandijk/b5f4e69d3d5f7b2942c5 to your computer and use it in GitHub Desktop.
Save jvandijk/b5f4e69d3d5f7b2942c5 to your computer and use it in GitHub Desktop.
Ordering with WP REST API
<?php
add_filter('rest_post_query', [$this, 'sortByDate'], 10, 2);
public function sortByDate($args, $request)
{
if ($args['post_type'] !== 'event') {
return $args;
}
$now = time();
$order = [
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => [
[
'key' => 'end_date',
'value' => ($now - ($now % 86400)),
'type' => 'DECIMAL',
'compare' => '>=',
]
]
];
return array_merge($args, $order);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment