kristopher gardham
kristopher gardham

Reputation: 31

Wordpress, PHP, Meta Query passing get / post variables

EDIT: I renamed all the fields so that name!= key[] now name = pool or whirlpool, and I'm doing them one at a time....

So I can get this to work:

 $args = array(
'numberposts'    => -1,
'category_name' => 'reviews',
'post_status'       => 'publish',
'meta_query'        =>  array(
    'relation'  => 'AND',
    array(
        'key'       => 'whirlpool',
        'value'     => '',
        'compare'   => '!='
    ),
 array(
        'key'       => 'pool',
        'value'     => '',
        'compare'   => '!='
    )
)
); 

$the_query = new WP_Query($args); 

but i cannot get the arraypush to work or this:

$query_array = array('relation' => 'AND');


if(isset($_GET['massage']) && !empty($_GET['massage'])){
$massage = $_GET['massage'];
array_push($query_array, array('key' => 'massage','value' => '','compare'=> '!='));
}
if(isset($_GET['facial']) && !empty($_GET['facial'])){
$facial = $_GET['facial'];
array_push($query_array, array('key' => 'facial','value' => '','compare'=> '!='));
}

if(isset($_GET['manicure']) && !empty($_GET['manicure'])){
$manicure = $_GET['manicure'];
array_push($query_array, array('key' => 'manicure','value' => '','compare'=> '!='));
}

 $the_query = new WP_Query( array( 
                'numberposts' => -1,
                'category_name' => 'reviews',
                'post_status'       => 'publish',
                'query' => $query_array ) 
            );

I feel like I've got this figured out in my head, but I can't seem to knock it out. Please don't worry about sanitization and what not... --

I want to post variables from checkboxes, and then draw on them to search for specific posts by meta key (the value isn't important so long as it exists as something.. isset() I guess.

HTML

<li><input type="checkbox" name="key[]" value="pool" id="pool"><label class="checkbox" for="pool"> Pool </label></li>

<li><input type="checkbox" name="key[]" value="whirlpool" id="whirlpool"><label class="checkbox" for="whirlpool"> Whirlpool </label></li>

Desired PHP

$args = array(
'numberposts' => -1,
'category_name' => 'reviews',      
'meta_query' => array(
    'relation' => 'AND',
    array(
        'key' => 'pool',
    ),
    array(
        'key' => 'whirlpool',
    )
)

);

can i simply pass a sanitized $_GET['key'] as an array like this..

    array(
        'key' => '$_GET['key']['0']',
    ),
    array(
        'key' => '$_GET['key']['1']',
    )

If so, i'm not entirely sure how to use the foreach loop in combination with the WP_Query args.

I apologize in advance for the formatting of this.

Upvotes: 0

Views: 956

Answers (1)

Domain
Domain

Reputation: 11808

Try this -

$arr = array('relation' => 'AND');

foreach($_GET['key'] as $key){
  $arr[] = array('key' => $key);
}

and then use it in meta query -

$args = array(
'numberposts' => -1,
'category_name' => 'reviews',      
'meta_query' => $arr
);

Upvotes: 0

Related Questions