/home/bdqbpbxa/rc-subdomains/optoman-rc.goodface.com.ua/wp-content/themes/optoman/search.php
<?php
get_header();
$search_query = get_search_query();
// === Основні параметри пошуку ===
$args = [
's' => $search_query,
'post_type' => [
'page','post','industry','whitepaper','event','certificate','project',
'representative','resource','review','technology','product'
],
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'relevance',
'suppress_filters'=> false,
];
$q = new WP_Query($args);
// Якщо Relevanssi активний — використовуємо його
if (function_exists('relevanssi_do_query')) {
relevanssi_do_query($q);
}
// Пріоритетність типів постів
$post_type_priority = [
'product' => 1,
'post' => 2,
'whitepaper' => 3,
'event' => 3,
'industry' => 3,
'project' => 3,
'representative' => 3,
'review' => 3,
'technology' => 3,
'available-lenses'=> 3,
'job' => 3,
'certificate' => 3,
'resource' => 3,
'page' => 3,
];
$items = [];
if ($q->have_posts()) {
while ($q->have_posts()) {
$q->the_post();
$id = get_the_ID();
$title = mb_strtolower(get_the_title());
$blob = get_post_meta($id, '_search_blob', true);
$content = mb_strtolower(wp_strip_all_tags(get_the_content()));
$combo = trim($title . ' ' . ($blob ?: '') . ' ' . $content);
// Токенізуємо пошуковий запит
$tokens = function_exists('rlv_tokenize_query') ? rlv_tokenize_query($search_query) : explode(' ', mb_strtolower($search_query));
// Підрахунок збігів
$total_matches = 0;
if (function_exists('rlv_count_occurrences')) {
foreach ($tokens as $t) {
$total_matches += rlv_count_occurrences($combo, $t);
}
} else {
foreach ($tokens as $t) {
$total_matches += substr_count($combo, $t);
}
}
if ($total_matches === 0) continue;
// Підсвітка заголовку
$renderTitle = function_exists('highlight_search_terms')
? highlight_search_terms(get_the_title(), $search_query)
: get_the_title();
// Контекст (якщо немає підсвітки в заголовку)
$renderContext = '';
$hasTitleHighlight = strpos($renderTitle, '<mark') !== false;
if (!$hasTitleHighlight && function_exists('get_search_context')) {
$ctx = get_search_context($id, $search_query);
if ($ctx) {
$renderContext = highlight_search_terms($ctx, $search_query);
}
}
$items[] = [
'id' => $id,
'link' => get_permalink(),
'title' => $renderTitle,
'context' => $renderContext,
'post_type' => get_post_type(),
'matches' => $total_matches,
];
}
wp_reset_postdata();
}
// === Групування результатів ===
$grouped_results = [];
$post_type_counts = [];
foreach ($items as $item) {
$pt = $item['post_type'];
if (!isset($grouped_results[$pt])) {
$grouped_results[$pt] = [];
$post_type_counts[$pt] = 0;
}
$grouped_results[$pt][] = $item;
$post_type_counts[$pt]++;
}
// === Сортування за пріоритетами ===
$sorted_groups = [];
foreach ($post_type_priority as $type => $priority) {
if (isset($grouped_results[$type])) {
$sorted_groups[$type] = [
'posts' => $grouped_results[$type],
'count' => $post_type_counts[$type],
'priority' => $priority,
];
}
}
// Додаємо невідомі типи
foreach ($grouped_results as $type => $posts) {
if (!isset($sorted_groups[$type])) {
$sorted_groups[$type] = [
'posts' => $posts,
'count' => $post_type_counts[$type],
'priority' => 999,
];
}
}
?>
<main>
<section class="search-section">
<div class="container">
<div class="search-section__wrapper">
<h1 class="title title-3">
SEARCH FOR „<?php echo esc_html($search_query); ?>“
</h1>
<p class="result-count body-3">
<?php echo count($items); ?> results
</p>
<?php if (!empty($items)) : ?>
<div class="search-results-grouped">
<?php foreach ($sorted_groups as $post_type => $group) : ?>
<div class="search-result-group">
<div class="search-result-group-header">
<?php echo esc_html($post_type); ?> (<?php echo $group['count']; ?>)
</div>
<?php foreach ($group['posts'] as $result) : ?>
<div class="search-result-item">
<a href="<?php echo esc_url($result['link']); ?>" class="search-result-link">
<div class="search-result-title">
<?php echo wp_kses($result['title'], ['mark'=>[]]); ?>
</div>
<div class="search-result-matches">
<?php echo intval($result['matches']); ?> matches
</div>
<?php if (!empty($result['context']) && trim($result['context']) !== '') : ?>
<div class="search-result-context">
<?php echo wp_kses($result['context'], ['mark'=>[]]); ?>
</div>
<?php endif; ?>
</a>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
<?php else : ?>
<p class="no-results">
No results could be found for "<?php echo esc_html($search_query); ?>".
</p>
<?php endif; ?>
</div>
</div>
</section>
</main>
<?php get_footer(); ?>