Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am creating a multiple filter - as in I could filter by all the options if I wanted to do so, i.e terms, calories & price in my case.

I am having trouble with meta query args. At present I can search individually by terms or calories, and multiply by terms and calories, and terms, calories and price together but price on it's own does not effect search results?

Select:

                <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>

Edit: Added complete code below Isset and new 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>';

Default query on page load:

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
                    )
            )
    )
share|improve this question
    
What is the value of $min and $max in your code? Also can you put the complete code in ISSET section? –  Chittaranjan Dec 14 '13 at 11:33
    
@Chittaranjan They are the entered form input values. The meta_query price values do get updated correctly, but the search result does not correspond (still shows everything). Thanks –  user1575949 Dec 14 '13 at 14:20
    
The query dump looks right. What is the format for price in the database? –  s_ha_dum Dec 14 '13 at 15:00
    
I still don't see $min, $max collected when building the $args array for WP_Query unless the above code is placed in the same file where you have the form code. Can you please check the price values and how they are stored in the database? –  Chittaranjan Dec 14 '13 at 15:02
    
@Chittaranjan "... code is placed in the same file ..." Yes it is. I have added comments for clarity. "how they are stored in the database?" Do you want to see the add_meta_boxes code? –  user1575949 Dec 14 '13 at 16:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.