php - If Statement Within A Nested Array -


what trying query wordpress custom post type using meta keys search form. search form takes user inputs , show results based on matching criteria, of form's fields might blank, need make sure don't pass blank value query argument. need use if within arguments array.

i'll grateful type of help.

this code using, getting error message.

parse error: syntax error, unexpected t_if, expecting ')'

here code:

        if (isset($post['stock']) && !empty($post['stock'])) {             $stock = $post['stock'];         }         if (isset($post['mineral']) && !empty($post['mineral'])) {             $mineral = $post['mineral'];         }         if (isset($post['species']) && !empty($post['species'])) {             $species = $post['species'];         }         if (isset($post['color']) && !empty($post['color'])) {             $color = $post['color'];         }         if (isset($post['chemicalclass']) && !empty($post['chemicalclass'])) {             $chemicalclass = $post['chemicalclass'];         }         if (isset($post['locality']) && !empty($post['locality'])) {             $locality = $post['locality'];         }         if (isset($post['description']) && !empty($post['description'])) {             $description = $post['description'];         }         if (isset($post['size']) && !empty($post['size'])) {             $size = $post['size'];         }         if (isset($post['pricegt'])) {             $pricegt = $post['pricegt'];         } else {             $pricegt = 0;         }         if (isset($post['pricelt'])) {             $pricelt = $post['pricelt'];         } else {             $pricelt = 999999;         }          $args = array(             'post_type'         => 'products',             'productspecies'    => $species,             'localities'        => $locality,             'meta_query' => array(                 //'relation' => 'or',                 array(                     'key' => '_price',                     'value' => array( $pricegt, $pricelt ),                     'compare' => 'between',                     'type' => 'numeric',                 ),                 if ($mineral) {                 array(                     'key' => '_components',                     'value' => $mineral,                 ),                 }                 if ($stock) {                 array(                     'key' => '_lotnum',                     'value' => $stock,                 ),                 }                 if ($color) {                 array(                     'key' => '_color',                     'value' => $color,                 ),                 }                 if ($description) {                 array(                     'key' => '_smalldesc',                     'value' => $description,                     'compare' => 'like',                 ),                 }                 if ($size) {                 array(                     'key' => '_size',                     'value' => $size,                 ),                 }                 if ($chemicalclass) {                 array(                     'key' => '_chemicalclass',                     'valeu' => $chemicalclass,                 ),                 }             ),             );     ?>         <?php $query = new wp_query( $args ); ?>          <div class="postcount">we found total of <span><?php echo $query->post_count;?></span> items maching search</div>      <?php if ( $query->have_posts() ) : ?>          <?php /* start loop */ ?>         <?php while ( $query->have_posts() ) : $query->the_post(); ?> 

what doing wrong?

you trying pass if statements arguments array() function. php not allow that. 1 thing can build array without optional parts , add them later if necessary:

if ($stock) {     $args['metaquery'][] = array(         'key' => '_lotnum',         'value' => $stock     ); } 

Comments