在wp_Query中按品牌名称搜索产品

时间:2020-04-17 作者:ilanb

我使用它按名称在两个类别上搜索产品:

<ul class="products">
                    <?php
                    $paged = ( get_query_var( \'paged\' ) ) ? absint( get_query_var( \'paged\' ) ) : 1;
                    // Request is empty, view all products based url cat ID
                    if($tmp == \'\')
                    {
                        $args = array(
                            \'post_type\'             => \'product\',
                            \'s\'                     => $tmp,
                            \'posts_per_page\'        => \'8\',
                            \'paged\'                 => $paged,
                            \'author\'                => $store_user->ID,
                            \'orderby\'               => \'menu_order\',
                            \'order\'                 => \'ASC\',
                            \'tax_query\'             => array(
                                    array(
                                        \'taxonomy\'      => \'product_cat\',
                                        \'field\'         => \'term_id\',
                                        \'terms\'         => $product_cat_id,
                                    ),
                            )
                        );
                    }
                    else // search all product on categorie 18 & 25
                    {
                        $args = array(
                            \'post_type\'             => \'product\',
                            \'s\'                     => $tmp,
                            \'posts_per_page\'        => \'8\',
                            \'paged\'                 => $paged,
                            \'author\'                => $store_user->ID,
                            \'orderby\'               => \'menu_order\',
                            \'order\'                 => \'ASC\',
                            \'tax_query\'             => array(
                                array(
                                    \'relation\' => \'OR\',
                                    array(
                                        \'taxonomy\'      => \'product_cat\',
                                        \'field\'         => \'term_id\',
                                        \'terms\'         => 18,
                                    ),
                                    array(
                                        \'taxonomy\'      => \'product_cat\',
                                        \'field\'         => \'term_id\',
                                        \'terms\'         => 25,
                                    ),
                                ),
                            )
                        );
                    }
                    $loop = new WP_Query( $args );
                    //var_dump($loop);

                    if ( $loop->have_posts() )
                    {}
如何添加查询参数以查看品牌产品?

因为当我在搜索栏中写下品牌名称时,找不到任何产品。

谢谢

1 个回复
SO网友:ilanb

Founded:

$filter_group_a = array(
                            \'relation\' => \'OR\',
                            array(
                                \'taxonomy\'      => \'product_cat\',
                                \'field\'         => \'term_id\',
                                \'terms\'         => 18,
                            ),
                            array(
                                \'taxonomy\'      => \'product_cat\',
                                \'field\'         => \'term_id\',
                                \'terms\'         => 25,
                            ),
                        );

                        $filter_group_b = array(
                            array(
                                \'taxonomy\' => \'product_brand\',
                                \'field\'    => \'description\',
                                \'value\'    => $tmp,
                            ),
                        );


                        $args = array(
                            \'post_type\'             => \'product\',
                            \'s\'                     => $tmp,
                            \'posts_per_page\'        => \'8\',
                            \'paged\'                 => $paged,
                            \'author\'                => $store_user->ID,
                            \'orderby\'               => \'menu_order\',
                            \'order\'                 => \'ASC\',
                            \'tax_query\'             => array(
                                array(
                                    \'relation\' => \'OR\',
                                    $filter_group_a,
                                    $filter_group_b,
                                ),
                            )
                        );
                    }

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post