to group search results by Category. Each category should have it ranking:
Search result page:
Persons: (results from category "Persons", by relevance) 1 2 3 pagination
Companies (results from category "Companies", by relevance)
News (results from category "News", by date (newest first)
Here is my code so far:
<?php
get_header();
// Get search visibility option from admin settings
$gecko_settings = GeckoConfigSettings::get_instance();
// Options page settings
$full_width_layout = $gecko_settings->get_option('opt_search_full_width_layout', 0);
// $builder_friendly_layout = get_post_meta(get_proper_ID(), 'gecko-page-builder-friendly', true);
$main_class = 'both';
if ($gecko_settings->get_option('opt_sidebar_left_search_vis', 1) == 1) {
$main_class = 'main--left';
}
if ($gecko_settings->get_option('opt_sidebar_right_search_vis', 1) == 1) {
$main_class = 'main--right';
}
if ($gecko_settings->get_option('opt_sidebar_left_search_vis', 1) == 1 && $gecko_settings->get_option('opt_sidebar_right_search_vis', 1) == 1) {
$main_class = 'main--both';
}
$content_id = "";
if ($gecko_settings->get_option('opt_search_grid', 0) == 1) {
$content_id = "gecko-blog";
}
?>
<div id="main" class="main <?php echo $main_class;
if ($full_width_layout === 1):
echo " main--full"; endif; ?>">
<!-- ABOVE CONTENT WIDGETS -->
<?php get_template_part('template-parts/widgets/above-content'); ?>
<!-- end: ABOVE CONTENT WIDGETS -->
<div <?php if ($content_id): ?>id="<?php echo $content_id; ?>" <?php endif; ?> class="content">
<?php
if (function_exists('yoast_breadcrumb')) {
if ($gecko_settings->get_option('opt_yoastseo_breadcrumbs', 1) == 1) {
yoast_breadcrumb('<div id="breadcrumbs" class="gc-breadcrumbs">', '</div>');
}
}
?>
<?php
// Group search results by category
$search_categories = array(
'Persons' => array(),
'Companies' => array(),
'News' => array(),
);
if (have_posts()):
while (have_posts()):
the_post();
$category = get_the_category();
if (count($category) > 0) {
$category_name = $category[0]->cat_name;
if (array_key_exists($category_name, $search_categories)) {
$search_categories[$category_name][] = $post;
}
}
endwhile;
endif;
?>
<?php if (count($search_categories['Persons']) > 0): ?>
<h2>Persons:</h2>
<ol>
<?php
// Sort Persons results by relevance
uasort($search_categories['Persons'], function ($a, $b) {
$relevance_a = get_post_meta($a->ID, 'relevance', true);
$relevance_b = get_post_meta($b->ID, 'relevance', true);
if ($relevance_a == $relevance_b) {
return 0;
}
return ($relevance_a > $relevance_b) ? -1 : 1;
});
foreach ($search_categories['Persons'] as $post): ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ol>
<?php the_posts_pagination(); ?>
<?php endif; ?>
<?php if (count($search_categories['Companies']) > 0): ?>
<h2>Companies:</h2>
<ol>
<?php
// Sort Companies results by relevance
uasort($search_categories['Companies'], function ($a, $b) {
$relevance_a = get_post_meta($a->ID, 'relevance', true);
$relevance_b = get_post_meta($b->ID, 'relevance', true);
if ($relevance_a == $relevance_b) {
return 0;
}
return ($relevance_a > $relevance_b) ? -1 : 1;
});
foreach ($search_categories['Companies'] as $post): ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ol>
<?php the_posts_pagination(); ?>
<?php endif; ?>
<?php if (count($search_categories['News']) > 0): ?>
<?php
// Sort News results by date (newest first)
usort($search_categories['News'], function ($a, $b) {
$time_a = strtotime($a->post_date);
$time_b = strtotime($b->post_date);
if ($time_a == $time_b) {
return 0;
}
return ($time_a > $time_b) ? -1 : 1;
});
?>
<h2>News:</h2>
<ol>
<?php foreach ($search_categories['News'] as $post): ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ol>
<?php the_posts_pagination(); ?>
<?php endif; ?>
<?php
// If no content, include the "No posts found" template.
if (!have_posts()):
get_template_part('template-parts/content', 'none');
endif;
?>
</div>
<!-- UNDER CONTENT WIDGETS -->
<?php get_template_part('template-parts/widgets/under-content'); ?>
<!-- end: UNDER CONTENT WIDGETS -->
Here is what I trying to get:
- To make the search results clickable
- To make search results correct. It's return now something not related to request.