Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////
- // Alphabetize Search Results from Relevanssi
- // Group Projects, Posts
- /////////////////////////////////////////////////////////////
- add_filter('relevanssi_hits_filter', 'order_the_results');
- function order_the_results($hits) {
- global $wp_query;
- $alphabetize = array();
- if (!empty($hits)) {
- foreach ($hits[0] as $hit) {
- $alphavalue = get_post_meta($hit->ID, 'alphabetize', true);
- if (!isset($alphabetize[$alphavalue])) $alphabetize[$alphavalue] = array();
- array_push($alphabetize[$alphavalue], $hit);
- }
- // sortby ascending
- ksort($alphabetize);
- $sorted_hits = array();
- foreach ($alphabetize as $alphavalue => $alpha_hits) {
- $sorted_hits = array_merge($sorted_hits, $alpha_hits);
- }
- $hits[0] = $sorted_hits;
- // split post_types to show projects first
- $types['project'] = array();
- $types['post'] = array();
- foreach ($hits[0] as $hit) {
- if (!is_array($types[$hit->post_type])) $types[$hit->post_type] = array();
- array_push($types[$hit->post_type], $hit);
- }
- // Merge back to $hits in the desired order
- $hits[0] = array_merge($types['project'], $types['post']);
- }
- return $hits;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement