如果你想要用户通过一个特定的搜索表单来搜索产品,可以在你的主题的 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&hellip;', '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 将被包含在查询字符串中。