Created
November 3, 2015 13:27
-
-
Save jvandijk/b5f4e69d3d5f7b2942c5 to your computer and use it in GitHub Desktop.
Ordering with WP REST API
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 | |
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