首页 > 解决方案 > woocommerce 产品的自定义 WP 查询

问题描述

获取参数是查询字符串并在 wp 查询中使用它们,还使用税和元查询过滤器需要创建排序方式:使用 wp 查询的流行、日期和价格我在查询字符串中得到所有内容,所以很困惑我应该如何排序或排序使用查询字符串

if($starting_amount || $ending_amount || $animal || $activity || $hunting_method || $arrival_date || $departure_date || $product_cat || $orderby):
                            $args = array(
                                'post_type' => 'product',
                                'post_status' => 'publish',
                                'order' => 'DESC',
                                'orderby'   => 'meta_value',
                                'tax_query' => array(
                                    'relation' => 'AND',
                                    array(
                                        'taxonomy'  => 'animal',
                                        'terms'  => $animal,
                                        'field'  => 'id',
                                        'include_children' => true,
                                        'operator' => ($animal != '') ? 'IN' : 'NOT IN',
                                    ),
                                    array(
                                        'taxonomy'  => 'activity',
                                        'terms'  => $activity,
                                        'field'  => 'id',
                                        'include_children' => true,
                                        'operator' => ($activity != '') ? 'IN' : 'NOT IN',
                                    ),
                                    array(
                                        'taxonomy'  => 'hunting-method',
                                        'terms'  => $hunting_method,
                                        'field'  => 'id',
                                        'include_children' => true,
                                        'operator' => ($hunting_method != '') ? 'IN' : 'NOT IN',
                                    ),
                                    array(
                                        'taxonomy'  => 'product_cat',
                                        'terms'  => $product_cat,
                                        'field'  => 'slug',
                                        'include_children' => true,
                                        'operator' => ($product_cat != '') ? 'IN' : 'NOT IN',
                                    ),
                                ),
                                'meta_query' => array(
                                    'relation' => 'OR',
                                    array(
                                        'key' => '_price',
                                        'value' => array($starting_amount, $ending_amount),
                                        'compare' => 'BETWEEN',
                                        'type' => 'NUMERIC'
                                    ),
                                    array(
                                        'key' => 'total_sales',
                                        'value' => array($starting_amount, $ending_amount),
                                        'compare' => 'BETWEEN',
                                        'type' => 'NUMERIC'
                                    ),
                                    array(
                                        'relation' => 'OR',
                                        array(
                                            'key' => 'arrival_date',
                                            'value' => $new_arrival_date,
                                            'meta_compare'=> ($arrival_date != '') ? '==' : '>=',
                                            'type'=>'DATE',
                                        ),
                                        array(
                                            'key' => 'departure_date',
                                            'value' => $new_departure_date,
                                            'meta_compare'=> ($departure_date != '') ? '==' : '>=',
                                            'type'=>'DATE',
                                        )
                                    )
                                ),
                                'orderby' => 'date',
                                'posts_per_page' => -1,
                            );
                        else:
                            $args = array(
                                'post_type' => 'product',
                                'post_status' => 'publish',
                                'order' => 'DESC',
                                'orderby' => 'date',
                                'posts_per_page' => -1,
                            );
                        endif;

只需要按功能排序或排序

标签: phpwordpress

解决方案


推荐阅读