Title: Conditional statement within an array
Last modified: August 24, 2016

---

# Conditional statement within an array

 *  Resolved [jduffell](https://wordpress.org/support/users/jduffell/)
 * (@jduffell)
 * [11 years ago](https://wordpress.org/support/topic/conditional-statement-within-an-array/)
 * Hi I have the following array, but really I only want to query the gender if 
   the $gender variable exists, as conditional statements can’t be located within
   a php array, are there any clever / obvious suggestions I maybe overlooking on
   how to go about this?
 *     ```
       $args  = array(
       	'role'		=> 'talent',
       	'order'		=> 'DESC',
       	'orderby'	=> 'user_registered',
       	'meta_key'	=> 'position',
       	'number'	=> '15',
       		'meta_query'=> array (
       			array(
       				'key'		=> 'picture',
       				'value'   => array(''),
       				'compare' => 'NOT IN'
       			),
       			array(
       				'key'		=> 'gender',
       				'value'		=> $gender
       			),
       		)
       	);
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [11 years ago](https://wordpress.org/support/topic/conditional-statement-within-an-array/#post-6113077)
 * Hi jduffell
 * Try this:
 *     ```
       $args  = array(
       	'role'       => 'talent',
       	'order'      => 'DESC',
       	'orderby'    => 'user_registered',
       	'meta_key'   => 'position',
       	'number'     => '15',
       	'meta_query' => array (
       		array(
       			'key'     => 'picture',
       			'value'   => array( '' ),
       			'compare' => 'NOT IN'
       		),
       	),
       );
   
       if ( $gender ) {
       	$args['meta_query'][] = array(
       		'key'  => 'gender',
       		'value'  => $gender
       	);
       }
       ```
   
 *  Thread Starter [jduffell](https://wordpress.org/support/users/jduffell/)
 * (@jduffell)
 * [11 years ago](https://wordpress.org/support/topic/conditional-statement-within-an-array/#post-6113081)
 * [@keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Cheers for that, I was totally overlooking this.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Conditional statement within an array’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [jduffell](https://wordpress.org/support/users/jduffell/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/conditional-statement-within-an-array/#post-6113081)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
