How to create this query properly?

I have the following code to WP_Query. But I am not much good in PHP and WordPress, so please help me to solve this.

$args = array(
    'post_type' => 'job', 
    (isset($gender)) ? 
        ('meta_key'  => 'gender',
        'meta_value'     => $gender)
    : '');
 $query = new WP_Query( $args );

But its showing this error and I am not sure, how to format it.

Read More
Parse error: syntax error, unexpected T_DOUBLE_ARROW in XXX

How can I format it to make array like this.

$args= array( 'post_type' => 'job',
              'meta_key' => 'gender',
              'meta_value'  => $gender
       );

Related posts

1 comment

  1. Try this :

    <?php 
        $args = array('post_type' => 'job');
        if(isset($gender)){
            $args['meta_key'] = 'gender';
            $args['meta_value'] = $gender;
        }
        $query = new WP_Query( $args );
    ?>
    

Comments are closed.