WP Query multiple select form – Справка meta_query

Я создаю несколько фильтров, так как я мог бы фильтровать все параметры, если бы захотел, т. Е. Условия, калории и цена в моем случае.

У меня возникают проблемы с мета-запросами. В настоящее время я могу искать индивидуально по срокам или калориям и умножать по срокам и калориям, а также срокам, калориям и цене вместе, но цена на нее не влияет на результаты поиска?

Выбрать:

<form action="/?page_id=5" method="post"> <?php // Set min/max variables with post input data $min = $_POST['min']; // Collect min value and set to var $max = $_POST['max']; // Collect max value and set to var // Set filter var with term post input data $product_filter = $_POST['product_terms']; $cal_filter = $_POST['product_cal']; // Set term parameters/arguments $args = array( 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false, 'parent' => 0, ); // Get terms with our arguments $shop_terms = get_terms('shop', $args); echo '<select name="product_terms[]" multiple>'; // Set var as an array $all_product_terms = array(); foreach($shop_terms as $shop_term){ // Set all terms to our array $all_product_terms[] = $shop_term->name; echo '<option value="'.$shop_term->name.'">'.$shop_term->name.'</option>'; } echo '</select>'; $all_product_cal = array(); $post_args = array( 'post_type' => 'shop_post', 'order' => 'ASC', 'posts_per_page' => -1, ); $posts = get_posts($post_args); echo '<select name="product_cal[]" multiple>'; foreach ($posts as $post){ $keys = get_post_meta( $post->ID, 'calories' ); // check if the custom field has a value $all_product_cal[] = $keys[0]; if( ! empty( $keys ) ) { echo '<option value="'.$keys[0].'">'.$keys[0].' cal</option>'; } } echo 'From: &pound; <input id="shop1" type="text" name="min">'; echo 'To: &pound; <input id="shop2" type="text" name="max">'; echo '</select>'; echo '<input type="submit" value="go">'; ?> </form> 

Изменить: добавлен полный код ниже Isset и новый WP_Query:

  $final_product_term = isset($_POST['product_terms']) ? $product_filter : $all_product_terms; $final_product_cal = isset($_POST['product_cal']) ? $cal_filter : $all_product_cal; $min_value = isset($_POST['min']) ? $min : '1'; $max_value = isset($_POST['max']) ? $max : '100000'; $args = array( 'post_type' => 'shop_post', 'order' => 'ASC', 'post_status' => 'any', 'orderby' => 'name', 'tax_query' => array( array( 'taxonomy' => 'shop', 'field' => 'slug', 'terms' => $final_product_term ) ), 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'calories', 'value' => $final_product_cal, ), array( 'key' => 'price', 'value' => array($min_value, $max_value), 'type' => 'numeric', 'compare' => 'BETWEEN' ) ) ); $shop_posts = new WP_Query($args); if($shop_posts->have_posts()) : while($shop_posts->have_posts()) : $shop_posts->the_post(); $met = get_post_meta( $post->ID, 'price'); foreach($met as $key => $val ); echo '<div class="span3">'; echo '<div class="inner-shop">'; echo get_the_post_thumbnail($post->ID, 'blog-cat'); echo '<p class="product-title">'. get_the_title($post->ID).'</p>'; echo '<p class="product-title">£'. $val.'</p>'; echo '</div>'; echo '</div>'; 

Запрос по умолчанию при загрузке страницы:

 WP_Query Object ( [query] => Array ( [post_type] => shop_post [order] => ASC [post_status] => any [orderby] => name [tax_query] => Array ( [0] => Array ( [taxonomy] => shop [field] => slug [terms] => Array ( [0] => Ball [1] => Bars [2] => Biscuits [3] => Chips [4] => Crackers [5] => Crisps [6] => Fudge [7] => Granola ) ) ) [meta_query] => Array ( [relation] => OR [0] => Array ( [key] => calories [value] => Array ( [0] => 1 [1] => 66 [2] => 56 [3] => 99 [4] => 70 [5] => 34 [6] => 50 [7] => 44 ) ) [1] => Array ( [key] => price [value] => Array ( [0] => 1 [1] => 100000 ) [type] => numeric [compare] => BETWEEN ) ) )