• Resolved jduffell

    (@jduffell)


    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

    (@keesiemeijer)

    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

    (@jduffell)

    @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.