How to add date_query to meta_query array

I am building a meta query array and all fields for the user are optional so some might not have any data. But there is also an option which requires a date query. I am still checking to see if this is working but not sure as of yet. Is this correct and if not, what would be the right way to do it? Not sure if there needs to be an OR condition in here somewhere.

$meta_query = [];
$date_query = [];

if ( $gender != '' ) {
    $meta_query[] = [
        'key' = 'gender',
        'value' = $gender,
        'compare' = '='
    ];
}

if ( $age != '' ) {
     $meta_query[] = [
        'key' = 'age',
        'value' = $age,
        'compare' = '='
    ];
}

if ( $userDate != 0 ) {
    $date_query[] = [
        'year' = $year,
        'month' = $month
    ];
}

$args = [
    'post_type' = 'person',
    'fields' = 'ids',
    'posts_per_page' = -1,
    'post_status' = 'any',
    'meta_query' = $meta_query,
    'date_query' = $date_query

];

$data = get_posts($args);

Topic date-query meta-query get-posts wp-query Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.