如何按相关性和日期降序对搜索结果进行排序? (Ajax 不会覆盖)

问题描述 投票:0回答:1

如何在我的 search.php 中添加一个片段,该片段将按相关性和 ndate 降序加载我们网站的搜索结果? (这样我就可以根据需要手动设置日期来订购结果。)问题是目前它似乎没有遵循任何参数。 Ajax search lite 的实时搜索显示正确的结果;然而,实际结果页面却没有。插件上的覆盖功能不起作用,所以我想我需要编辑 search.php 本身。

问题的一个例子是,如果您在我们的网站上搜索“S10”。前 8-10 个结果是随机产品,例如“S10 的抽屉更换”或“蓝色纸张重量(s10、s11)”,而我需要的顶部结果是“S10 工具箱,8 个门”。您可能会认为它首先出现,因为它在标题中首先包含搜索关键字...同样,实际的实时搜索结果对内容进行了正确的排序。它只是主要/实际搜索结果页面。

任何解决此问题的帮助将不胜感激。我们的搜索功能遇到的第一个问题是,提取结果大约需要 30 秒到一分钟。但是,我通过禁用 Ajax Search lite 中的某些设置来解决这个问题,导致我们的主题和插件都查询结果,从而导致严重的滞后。这个速度是固定的;然而,我们在实际搜索结果页面中看到的结果并没有以任何连贯的方式排序。我真的很感谢任何能在这方面帮助我的人。非常感谢您提供的任何见解。

我将在下面粘贴我们的 search.php,以便您可以看到我正在使用的内容:


<?php
/**
 * The template for displaying search results pages
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
 *
 * @package
 * @subpackage 2022 Theme
 * @since 1.0.0
 */

get_header();
?>

<main id="main-content" class="main-content light product-archive-main search-template-main">

    <!-- // ------- Search Results - borrowed from Product Archive -->
    <section class="product-archive-section">
        <div class="layer-over container grid-container product-archive-content-container">
            <nav class="grid-item desktop-3 product-archive-menu desktop-menu" aria-label="All Products">
                <?php
                wp_nav_menu( array(
                    'theme_location' => 'products-menu',
                    'menu' => 'Sonic Tools Store Menu'
                    )
                ); ?>
                <p class="caption product-archive-rep-callout">
                    <button class="simple-text-link footer-sales-link sales-form-trigger">Speak with a Sonic Rep</button> to discuss business solutions or to get a quote
                </p>
            </nav>
            <div class="grid-item desktop-9 product-archive-content">
                <?php
                    $search_string = get_search_query();
                    $args = array(
                        's'              => $search_string,
                        'posts_per_page' => 24,
                        'order'          => 'DESC',
                    );
                    $search_posts = new WP_Query( $args );
                ?>
                <?php if ( $search_posts->have_posts() ) : ?>
                    <div class="product-archive-heading search-heading results">
                        <h1 class="section-heading no-margin-bottom bc-product-archive__title product-archive-headline">
                            <?php printf( esc_html__( 'Search Results for: %s', 'my-theme' ), '<span>' . get_search_query() . '</span>' ); ?>
                        </h1>
                    </div>
                    <div class="product-archive-filters">
                        <nav class="product-archive-filter-chip-wrapper" aria-label="Subcategories">
                            <small class="prodocut-archive-small-label product-archive-filter-chips-label">Jump to:</small>
                            <ul class="product-archive-filter-chips">
                                <li class="product-archive-filter-chip product-all-menu-item">
                                    <button class="filter-chip simple product-all-menu-prompt" aria-controls="productsPocket" aria-expanded="false">
                                        <span class="chip-text">All Products</span>
                                    </button>
                                </li>
                                <li class="product-archive-filter-chip">
                                    <a class="filter-chip simple product-archive-filter-chip-link" href="/products/categories/toolsets">
                                        <span class="chip-text">Complete Toolsets</span>
                                    </a>
                                </li>
                                <li class="product-archive-filter-chip">
                                    <a class="filter-chip simple product-archive-filter-chip-link" href="/products/categories/toolboxes">
                                        <span class="chip-text">Toolboxes</span>
                                    </a>
                                </li>
                                <li class="product-archive-filter-chip">
                                    <a class="filter-chip simple product-archive-filter-chip-link" href="/products/categories/toolboxes">
                                        <span class="chip-text">Modular Cabinets</span>
                                    </a>
                                </li>
                                <li class="product-archive-filter-chip">
                                    <a class="filter-chip simple product-archive-filter-chip-link" href="/products/categories/categories/sonic-foam-system">
                                        <span class="chip-text">Sonic Foam System</span>
                                    </a>
                                </li>
                                <li class="product-archive-filter-chip">
                                    <a class="filter-chip simple product-archive-filter-chip-link" href="/products/categories/categories/hand-tools">
                                        <span class="chip-text">Hand Tools</span>
                                    </a>
                                </li>
                            </ul>
                        </nav>
                    </div>
                    <ul class="product-content-grid grid-col-4 search-items">
                        <?php
                            while ( $search_posts->have_posts() ) : $search_posts->the_post();
                                $postID = get_the_ID();
                                $featuredImg = wp_get_attachment_image_src(get_post_thumbnail_id($postID));
                                $hero = $featuredImg ? $featuredImg[0] : get_stylesheet_directory_uri().'/assets/images/post-thumbnail-fallback.jpg'; ?>
                                <li class="search-item">
                                    <div class="content-card article-card search-content-card">
                                        <picture class="article-card-hero search-content-card-hero" aria-hidden="true">
                                            <img class="article-card-hero-image search-content-card-hero-image" src="<?php echo $hero; ?>" loading="lazy" alt="<?php the_title(); ?>" width="400" height="200" />
                                        </picture>
                                        <div class="article-card-content search-content-card-inner">
                                            <h3 class="article-card-heading search-content-card-heading"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                            <p class="article-card-description"><?php echo get_the_excerpt(); ?></p>
                                            <a href="<?php the_permalink(); ?>" class="article-card-cta">
                                                <span class="secondary-button dark-text article-card-cta-button search-card-cta-button">
                                                    <span class="button-text">View</span>
                                                </span>
                                            </a>
                                        </div>
                                    </div>
                                </li>
                        <?php endwhile; ?>
                    </ul>
                    <div class="product-content-pagination">
                        <?php the_posts_navigation(); ?>
                    </div>
                <?php else : ?>
                    <div class="product-archive-heading search-heading no-results">
                        <h1 class="section-heading no-margin-bottom bc-product-archive__title product-archive-headline">
                            <?php printf( esc_html__( 'No Results for: %s', 'my-theme' ), '<span>' . get_search_query() . '</span>' ); ?>
                        </h1>
                        <p class="intro-copy empty-search-state-message">No products or content matches you search. For products, try using the menu here to find a relevant category.</p>
                        <a class="button primary-button" href="/products">
                            <span class="button-text">View all Products</span>
                        </a>
                    </div>
                <?php endif; ?>

                <?php wp_reset_postdata(); ?>
            </div>
        </div>
    </section>
</main>

<!-- // ------- Mobile Pocket -->
<div class="modal-overlay product-pocket-close product-archive-pocket-overlay" aria-hidden="true"></div>
<div id="productsPocket" class="product-archive-pocket" aria-live="assertive" aria-hidden="true">
    <div class="product-archive-pocket-inner">
        <div class="product-archive-pocket-nav-heading">
            <h2 class="product-archive-pocket-nav-title">Tools & Storage Solutions</h2>
            <button id="productsPocketClose" class="close-button product-pocket-close product-archive-pocket-close">
                <div class="close-button-inner">
                    <span class="close-bar" aria-hidden="true"></span>
                    <span class="close-bar" aria-hidden="true"></span>
                    <span class="screen-reader-text">Close Products Menu</span>
                </div>
            </button>
        </div>
        <nav class="product-archive-pocket-nav-body product-archive-menu mobile-menu" aria-label="All Products">
            <?php
            wp_nav_menu( array(
                'theme_location' => 'products-menu',
                'menu' => 'Sonic Tools Store Menu'
                )
            ); ?>
        </nav>
    </div>
</div>


<?php
get_footer();

我已经尝试了 Ajax Search Lite 中的所有选项。我尝试通过插件覆盖结果。这不是搜索插件能解决的事情。所以我想我需要编辑实际的search.php。我不想在不知道我会在此处添加正确功能的情况下进行任何更改。

ajax wordpress wordpress-theming searchbar
1个回答
0
投票

有很多插件可以潜在地解决这个问题,但如果你想用 WP_Query 来做到这一点,你需要更改

$args
。 WordPress 默认情况下按 relevance 提供顺序,定义为
Order by search terms in the following order: First, whether the entire sentence is matched. Second, if all the search terms are within the titles. Third, if any of the search terms appear in the titles. And, fourth, if the full sentence appears in the contents.

如果它符合您的目的,您需要按如下方式更改 $args:

$args = array(
    's'              => $search_string,
    'posts_per_page' => 24,
    'orderby' => array( 'relevance' => 'ASC', 'date' => 'DESC' )
);
© www.soinside.com 2019 - 2024. All rights reserved.