I've looked around at a few places and could not find an answer. I was wondering what the best way to create a WP_query from possibly empty variables were. I have about 16 of these $_GET variables.

if( isset($_GET['promotion']) ){
    $promotion = $_GET['promotion'];

    if($promotion == 'promotion'){
        $promo = 1;
    } else if($promotion == 'regulier'){
        $promo = 0;

In some cases $promo might not be set. So how can I create a meta_query let's say with dependant on if $promo is set or not.

'meta_query' => array(
        'key' => 'model-promotion',
        'value' => $promotion

If the meta_query is not set but present in my WP_query() the query breaks. Thanks for the help!

closed as off-topic by Chip Bennett, tfrommen, Tom J Nowell Feb 24 '14 at 16:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.


Just build the static part of the args array, then add the conditional variables, then pass the args array to WP_Query():

// Static args
$custom_query_args = array( /* static args here */ );

// Conditional arg
if ( 1 == $promo ) {
    $custom_query_args['meta_query'] = array( /* meta query array */ );

// Instantiate query
$custom_query = new WP_Query( $custom_query_args );

Not the answer you're looking for? Browse other questions tagged or ask your own question.