创建仅使用WooCommerce销售产品进行搜索的搜索表单

时间:2021-09-28 作者:moorewebx

我正在尝试创建一个搜索表单,该表单将在WooCommerce内正在销售/促销的产品中进行搜索。以下是我目前掌握的情况:

<form method="get" action="<?php echo esc_url( home_url( \'/\' ) ); ?>">
    <input type="hidden" post_type="product" post__in="wc_get_product_ids_on_sale" />
    <input type="text" size="16" name="s" value="Search"  />
    <input type="submit" value="Go" />
</form>

1 个回复
SO网友:Maulik Paddharia

是的,您可以在获取post查询时添加搜索值,如下所示:

$args = array(
    \'post_type\'      => \'product\',
    \'s\' => \'search value\',
    \'meta_query\'     => array(
        \'relation\' => \'OR\',
        array( // Simple products type
            \'key\'           => \'_sale_price\',
            \'value\'         => 0,
            \'compare\'       => \'>\',
            \'type\'          => \'numeric\'
        ),
        array( // Variable products type
            \'key\'           => \'_min_variation_sale_price\',
            \'value\'         => 0,
            \'compare\'       => \'>\',
            \'type\'          => \'numeric\'
        )
    )
);

query_posts( $args );

相关推荐

nothing happen in search form

我想创建搜索表单,但当我搜索时什么都没有发生,这是代码:索引。php: <div class=\"tech-btm\"> <?php get_search_form();?> </div> 搜索表单:<form role=\"search\" method=\"get\" id=\"searchform\" action=\"<?php echo home_url(\'/\')?>\"> &