Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Adds a custom field to all posts, with the unixtime of a custom date OR the published date
- *
- * @param int $postId
- */
- function customDateField($postId)
- {
- // Generate my unix time
- $customDate = get_field('date');
- $searchDate = ($customDate) ? date('U', strtotime($customDate)) : get_post_time('U', TRUE);
- // You never need to add the post_meta, this will add it if needed
- update_post_meta($postId, 'searchdate', $searchDate);
- remove_filter('save_post', 'customDateField');
- }
- add_filter('save_post', 'customDateField');
- /**
- * Allows the ordering by custom terms
- *
- * @param array $hits
- *
- * @return array mixed
- */
- function customDateSearch($hits)
- {
- if (get_query_var('orderby') == 'searchdate') {
- $dates = array();
- foreach ($hits[0] as $hit) {
- $searchdate = get_post_meta($hit->ID, 'searchdate', TRUE);
- if (!isset($dates[$searchdate])) {
- $dates[$searchdate] = array();
- }
- array_push($dates[$searchdate], $hit);
- }
- ksort($dates);
- $sorted_hits = array();
- foreach ($dates as $searchdate => $searchdate_hits) {
- $sorted_hits = array_merge($sorted_hits, $searchdate_hits);
- }
- // By default everything is sorted ascending, check if we need to change
- $hits[0] = ('asc' == strtolower(get_query_var('order'))) ? array_reverse($sorted_hits) : $sorted_hits;
- }
- return $hits;
- }
- add_filter('relevanssi_hits_filter', 'customDateSearch');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement