自定义WooCommerce PHP搜索与下拉


Custom WooCommerce PHP Search with Dropdown

我将以下代码添加到我网站上的php小部件中,以便通过下拉框按产品类别搜索网站。

<form role="search" method="get" id="searchform" action="<?phpbloginfo('siteurl'); ?>"<div>
<label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" />
<?php wp_dropdown_categories ($args = array(
'taxonomy' => 'product_cat',)); ?>
<input type="submit" id="searchsubmit" value="Search" /></div></form>

我不明白为什么搜索没有返回任何命中。当我看到搜索结果时,有些东西似乎不对劲。我只看到搜索词和类别号(example="?s=flour&cat=17")。如果有任何关于从哪里开始阅读的指导,我将不胜感激。

默认情况下,当执行搜索查询时,WordPress不搜索自定义帖子类型,为了解决您面临的问题,将以下隐藏字段添加到您的widgets表单定义

<input type="hidden" value="product" name="post_type">

使用pre_get_posts过滤器更改搜索查询

更多信息请查看下面的链接

https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts