【问题标题】:Products filters for get_posts functionget_posts 功能的产品过滤器
【发布时间】:2021-08-29 04:07:11
【问题描述】:

为 wooCommerce 产品创建搜索功能并观察函数 get_posts( $args = null ) 的工作原理,我看到像这样设置 args

      $args = array(
            's'                   => 'Some content',
            'post_type'           => 'product',
            'post_status'         => 'publish',
            'ignore_sticky_posts' => 1,
            'numberposts' => 999999, // set maximim value
            ...
        );

        $products_list = get_posts( $args );

1) 参数 's' 在 post_content 和 post_title 字段中进行搜索。如果有办法只在其中一个字段中搜索?

2) 我发现将“类别”参数设置为我需要的类别 - 它根本不起作用。 经过一些测试,我发现在 get_posts 函数中注释 'cat' 参数会使 'category' 参数起作用。 这个 'cat' 参数是干什么用的?

3) 在我的搜索中,我对价格、in_stock 字段进行过滤。为了实现这一点,我必须手动检查这两个标准。我的意思是我必须设置参数

'numberposts' => 999999, // 设置最大值为某个大数

并在循环中检查这些参数并跳过它们。我不能将这些参数设置为 get_posts 函数的过滤器吗?

如果我可以使用 get_posts 制作我需要的东西,或者如果有更好的工具,也许一些 wooCommerce 功能?

4) 我还通过添加参数进行了 sku 搜索:

            'meta_value'                   => '_sku',

看起来这个新条件作为 AND 工作,但我需要这个工作作为 OR 条件。 如果有办法做到吗?

谢谢!

【问题讨论】:

    标签: wordpress woocommerce posts


    【解决方案1】:

    元查询数组可以采用默认为“AND”的“relation”键:

        $args = array(
            's'                   => 'Some content',
            'post_type'           => 'product',
            'post_status'         => 'publish',
            'ignore_sticky_posts' => 1,
            'numberposts'         => -1,
            'meta_query'          => array(
                    'relation'    => 'OR',
                        array(
                        'key' => '_sku',
                        'value'   => 'some value',
                        'compare' => '='
                    ),
                    array(
                        'key'     => '_sku',
                        'value'   => 'another value',
                        'compare' => '='
                    )
            );
        );
    

    Alo,numberposts 可以将 -1 作为返回所有帖子的值。

    【讨论】:

      【解决方案2】:

      类似的东西可能同时针对 var,简单的产品

      $q2 = get_posts(array(
                                  'fields' => 'ids',
                                  'post_type' => array('product_variation', 'product'),
                                  'meta_query' => array(
                                      array(
                                              'key' => '_sku',
                                           'value' => 'valueInput',
                                          'compare' => 'LIKE'
                                      )
                                   )
                          ));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-02
        • 1970-01-01
        相关资源
        最近更新 更多