如果你想要用戶通過一個特定的搜索表單來搜索產品,可以在你的主題的 searchform.php 文件中修改搜索表單,添加一個隱藏字段來指定 post_type:
<form role="search" method="get" class="search-form" action="<?php echo esc_url(home_url('/')); ?>">
<label>
<span class="screen-reader-text"><?php echo _x('Search for:', 'label', 'your-theme-slug'); ?></span>
<input type="search" class="search-field" placeholder="<?php echo esc_attr_x('Search Products…', 'placeholder', 'your-theme-slug'); ?>" value="<?php echo get_search_query(); ?>" name="s" />
</label>
<input type="hidden" name="post_type" value="product" />
<button type="submit" class="search-submit"><?php echo esc_html_x('Search', 'submit button', 'your-theme-slug'); ?></button>
</form>
這樣,當用戶提交搜索表單時,post_type=product 將被包含在查詢字符串中。