首页 > 解决方案 > 强制 Woocommerce 按属性过滤以仅显示搜索词在标题中的属性

问题描述

我正在使用以下代码仅按产品标题进行 wordpress 搜索。但是,woocommerce“按属性过滤”小部件仍然显示过滤器,就好像搜索也在正文上完成一样。

因此,例如,如果我搜索“face”,则显示的结果都在标题中包含“face”,但 Filter By Attribute 小部件仍会将 Chocolate 作为属性显示,因为有一个巧克力棒的描述为“Face it, we都爱巧克力”

然后,如果您单击“巧克力”过滤器,它将不显示任何结果,因为搜索仅显示标题中带有“面孔”的结果。

我什至不知道从哪里开始寻找通过属性小部件更改过滤器的行为。

// Search titles only 
function __search_by_title_only( $search, $wp_query )
{
    global $wpdb;
    if(empty($search)) {
        return $search; // skip processing - no search term in query
    }
    $q = $wp_query->query_vars;
    $n = !empty($q['exact']) ? '' : '%';
    $search =
    $searchand = '';
    foreach ((array)$q['search_terms'] as $term) {
        $term = esc_sql($wpdb->esc_like($term));
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
    }
    if (!empty($search)) {
        $search = " AND ({$search}) ";
        if (!is_user_logged_in())
            $search .= " AND ($wpdb->posts.post_password = '') ";
    }
    return $search;
}
add_filter('posts_search', '__search_by_title_only', 500, 2);

标签: wordpresssearchwoocommercewidget

解决方案


我拼凑了我自己的过滤器,该过滤器与上述搜索(仅标题)配合得很好,并且没有显示任何具有标题中没有关键字的项目的品牌。我现在唯一的问题是弄清楚如何获得项目的数量。

<?php
 $searchterm = htmlentities($_GET["s"]);
 $filter_brand = htmlentities($_GET["filter_brand"]);
 $current = $_SERVER['REQUEST_URI'];
 $site_url = home_url();
 $args = array(
                'post_type' => 'product',
                'posts_per_page' => -1,
                'order' => 'ASC',
                'orderby' => 'title',
                's' => $searchterm
            );
    // The Query
  $the_query = new WP_Query( $args );



if( $the_query->have_posts() ) :
echo'<div id="woocommerce_layered_nav-3" class="widget woocommerce widget_layered_nav woocommerce-widget-layered-nav">
<ul class="woocommerce-widget-layered-nav-list">';
$unique_singler = array();
    while ( $the_query->have_posts() ) : $the_query->the_post();
     $brand_terms = get_the_terms( $post, 'pa_brand' );
     $singler = array_values($brand_terms)[0];
     $name = $singler->name;
     $name_string = strtolower($name);
     $name_slug = str_replace(" ","-",$name_string);
// only create option if city hasn't been added yet
        if( ! in_array( $name, $unique_singler ) ) :
            // add city to array so it doesn't repeat
            $unique_singler[] = $name;
       ;?>
    <li class="woocommerce-widget-layered-nav-list__item wc-layered-nav-term <?php if (!($filter_brand == $name_slug) && !empty($filter_brand)) { echo 'hidden'; }?>">
    <?php echo '<a class="remover ';
    if ($filter_brand == $name_slug) { echo 'shown'; }
    echo '" href="'.$site_url.'/?s='.$searchterm.'&tags=1&ixwps=1&post_type=product"><i class="fas fa-times"></i></a>';
 ?><?php
    echo '<a class="dup" href="'.$current.'&filter_brand='.$name_slug.'">'.$name.'</a> <span class="count">(' .count($XXXXXXX). ')</span>';
 ?></li>
            <?php
    endif;?>                
                            <?php endwhile;?>
                </ul>       
</div>              
                            <?php else :?>
    <p>Nothing to see</p>
<?php endif; ?>

 <style>
 li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term a.remover {
    display:none;
}

li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term a.remover.shown {
    display:inline-block;
}

li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term.hidden
{
    display:none;
}

.x-sidebar .widget ul li {
    border-top: 1px solid rgba(0,0,0,0.085) !important;
}

ul.woocommerce-widget-layered-nav-list
{
     border-top: 1px solid rgba(0,0,0,0.085) !important;
     border-bottom: 1px solid rgba(0,0,0,0.085) !important;
}

.woocommerce-widget-layered-nav-list, li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term {
    margin-left:0 !important;
}

 .x-sidebar .widget ul li a {
    padding-top: 8px;
    padding-bottom: 8px;
 }
 div#woocommerce_layered_nav-3 {
    margin: 0;
}

a.remover.shown i {
    text-decoration: underline !important;
    margin-right:0.5em;
}
</style>

推荐阅读