我正在尝试为基于woocommerce的网站构建一个高级搜索表单。
找到此答案(https://wordpress.stackexchange.com/a/184186/51376) 并添加了所有代码。但它不起作用。
功能。php
// advanced search functionality
function advanced_search_query($query) {
if($query->is_search()) {
// category terms search.
if (isset($_GET[\'product_cat\']) && !empty($_GET[\'product_cat\'])) {
$query->set(\'tax_query\', array(array(
\'taxonomy\' => \'product_cat\',
\'terms\' => array($_GET[\'product_cat\']) )
));
}
return $query;
}
}
add_action(\'pre_get_posts\', \'advanced_search_query\', 1000);
搜索表单;<form role="search" method="get" class="woocommerce-product-search" action="<?php echo esc_url( home_url( \'/\' ) ); ?>">
<div class="form-group">
<div class="search-form">
<label class="screen-reader-text" for="woocommerce-product-search-field"><?php _e( \'Search for:\', \'woocommerce\' ); ?></label>
<input type="search" id="woocommerce-product-search-field" class="search-field" placeholder="<?php echo esc_attr_x( \'Search Products…\', \'placeholder\', \'woocommerce\' ); ?>" value="<?php echo get_search_query(); ?>" name="s" title="<?php echo esc_attr_x( \'Search for:\', \'label\', \'woocommerce\' ); ?>" />
</div>
<?php wp_dropdown_categories( \'name=product_cat&hierarchical=1&taxonomy=product_cat&class=category-select&show_option_all=Tüm Kategoriler\' ); ?>
<div class="search-submit">
<input type="submit" value="<?php echo esc_attr_x( \'Search\', \'submit button\', \'woocommerce\' ); ?>" />
</div>
</div>
<input type="hidden" name="post_type" value="product" />
</form>