如何在自定义分类的产品中搜索

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

我创建了一个名为“product”的帖子类型和一个名为“product-cat”的自定义分类法。

我在 searchform.php 中创建了产品的一般搜索,并在 archive-product.php 中这样调用:

<div class="search-wrapper">
    <form role="search" method="get" id="searchform" action="<?php echo esc_url(site_url('products/')); ?>">
        <input type="text" value="" name="s" id="s" placeholder="Search products" />
        <input type="submit" id="searchsubmit" value="" />
    </form>
</div>

还有 search.php,我从 archive-product.php 复制了所有内容

问题是当分类产品搜索查询中的用户仅获取当前分类产品并在其中搜索(不是所有产品)时,我如何更改上面的代码。

我的分类模板文件是 taxonomy-product-cat.php

此外,如果我需要创建新的搜索模板文件,如 search-products-cat.php 或我需要执行该过程的任何内容,请提及。谢谢

php wordpress search taxonomy custom-taxonomy
1个回答
0
投票

为了满足您的要求:如果我需要创建新的搜索模板文件,如 search-products-cat.php 或其他...,请注意创建自定义搜索的两种常见方法是:

  1. 使用 searchform.php,和

  2. 使用 get_search_form 过滤器钩子。

下面的解决方案使用了 get_search_form 过滤器钩子。要对其进行测试,请将代码放入您的子主题或插件中,并在应显示表单的位置使用

get_search_form()
功能。调用
get_search_form()
触发 get_search_form 过滤器执行。

使用过滤器挂钩,您将无需创建多个模板搜索文件。但是,您需要使用适当的设计模式很好地组织您的代码,以处理各种情况(例如,针对不同

product-cat
术语的不同搜索页面设计)。使用 searchform.php.

时,您应该能够根据自己的目的轻松修改下面的示例代码

代码演示的基本思想是:

  1. 添加发送到前端的自定义搜索过滤器,以及

  2. 解析搜索过滤器,并将它们添加到后端的 WP_Query。

您可以通过多种方式添加自定义搜索过滤器。一种方法是使用

<input type="hidden" />
字段。提交搜索表单时,您可以通过标签名称解析隐藏字段,并将值添加到搜索查询中。示例代码如下。

get_search_form 过滤器钩子

/**
 * Render a custom search form.
 */
function custom_search_form( $form ) {
    // Post type plural name or the value configured for the `rewrite['slug']` register_post_type option
    $custom_post_type_plural = 'products';
    $custom_taxonomy = 'product-cat';

    $current_post_id = \get_the_ID();

    $related_term_ids = \wp_get_object_terms(
        $current_post_id,
        $custom_taxonomy,
        array(
            'fields' => 'ids'
        )
    );

    // home_url() is used by site visitors.
    // site_url() is the location of the WordPress installation.
    // They are often the same, but not always.
    $home_url = \home_url( $custom_post_type_plural ) . '/';

    if ( \is_singular() ) {
        $related_term_id_list = \esc_attr( implode( ',', $related_term_ids ) );
    } else {
        $related_term_id_list = '';
    }

    $search_query = \esc_attr( \get_search_query() );

    $form = <<<EOHTML
    <div class="search-wrapper">
        <form
            role="search"
            method="GET"
            id="searchform"
            action="$home_url"
        >
            <input type="text" name="s" id="s" placeholder="Search products" value="$search_query" />
            <input type="hidden" name="product_cat_term_list" value="$related_term_id_list" />
            <input type="submit" id="searchsubmit" value="Search" />
        </form>
    </div>
    EOHTML;

    return $form;
}

\add_filter( 'get_search_form', 'custom_search_form' );

pre_get_posts 动作挂钩

/**
 * Update the query variable 'tax_query' in the WP_Query object.
 * The action hook 'pre_get_posts' allows you to
 * update the query before it is executed.
 *
 * @see https://developer.wordpress.org/reference/hooks/pre_get_posts/#targeting-the-right-query
 *
 */
function target_product_cat_query_with_conditional_tags( $query ) {
    $custom_post_type = 'product';
    $custom_taxonomy = 'product-cat';
    $term_ids_form_field_name = 'product_cat_term_list';

    if ( ! $query->is_admin() &&
        $query->is_search() &&
        isset( $_GET[ $term_ids_form_field_name ] ) &&
        ! empty( $_GET[ $term_ids_form_field_name ] )
    ) {

        // Get the product-cat-term-ids and add them to the tax_query
        $related_term_id_list = $_GET[ $term_ids_form_field_name ];
        $related_term_ids = explode( ',', $related_term_id_list );

        $tax_query = array(
            array(
                'taxonomy' => $custom_taxonomy,
                'field' => 'term_id',
                'terms' => $related_term_ids
            )
        );
        $query->set( 'tax_query', $tax_query );
    }
}

add_action( 'pre_get_posts', 'target_product_cat_query_with_conditional_tags' );
© www.soinside.com 2019 - 2024. All rights reserved.