WP_Query Variable inside Array

The array below with “‘taxonomy’ => ‘age’” isn’t working, if I copy and paste the array “array( ’19’, ’20’ )” in place of the variable it works fine. I just started picking up PHP so go easy on me 🙂

                if($_REQUEST["price"] == 19){$price = "'19'";}
                elseif($_REQUEST["price"] == 20){$price = "array( '19', '20' )";}
                elseif($_REQUEST["price"] == 21){$price = "array( '19', '20', '21' )";}
                elseif($_REQUEST["price"] == 22){$price = "array( '19', '20', '21', '22' )";}
                elseif($_REQUEST["price"] == 23){$price = "array( '19', '20', '21', '22', '23' )";}
                echo $price;
                $my_query = new WP_Query( array(
                            'post_status' => 'publish',
                            'post_type' => 'post', 
                            'cat' => '' . $_REQUEST["category"] . '',
                            'tax_query' => array(
                                    'relation' => 'OR',
                                    array(
                                        'taxonomy' => 'age',
                                        'field' => 'id',
                                        'terms' => '' . $_REQUEST["age"] . ''
                                    ),
                                    array(
                                        'taxonomy' => 'price',
                                        'field' => 'id',
                                        'terms' => $price,
                                        'operator' => 'IN'
                                    ),
                                    array(
                                        'taxonomy' => 'group',
                                        'field' => 'id',
                                        'terms' => '' . $_REQUEST["group"] . ''
                                    )
                                ),
                            'posts_per_page' => '-1'
                            ) );

Related posts

Leave a Reply

1 comment

  1. Your array definitions aren’t correct. This…

    elseif($_REQUEST["price"] == 20){$price = "array( '19', '20' )";}
    

    … creates a literal string that reads “array( ’19’, ’20’ )”. Remove the quotes.

    elseif($_REQUEST["price"] == 20){$price = array( '19', '20' );}
    

    That should give you PHP arrays.