while loop in wp query array

2019-08-17 17:30发布

I got this wordpress array and need a while loop inside for every 'AND' relation:

$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
    'relation' => 'OR',
    array(
        'relation' => 'AND',
        array(
            'key'       => 'foo_0_start',
            'compare'   => '>=',
            'value'     => '$start'
        ),
        array(
            'key'       => 'foo_0_end',
            'compare'   => '<=',
            'value'     => '$end'
        )
    ),
    array(
        'relation' => 'AND',
        array(
            'key'       => 'foo_1_start',
            'compare'   => '>=',
            'value'     => '$start'
        ),
        array(
            'key'       => 'foo_1_end',
            'compare'   => '<=',
            'value'     => '$end'
        )
    )
)
);

I was searching for hours and tried to build a function without success. How can I accomplish this issue? And what happens with the "'relation' => 'OR',"?

$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array()
);

$i = 1;
while ($i<=5;) :
$i++
$query_args['meta_query'][] = array (
    'relation' => 'AND',
    array(
        'key'       => 'foo_$i_start',
        'compare'   => '>=',
        'value'     => '$start'
    ),
    array(
        'key'       => 'foo_$i_end',
        'compare'   => '<=',
        'value'     => '$end'
    )
);
endwhile;

Help would be highly appreciated.

1条回答
我命由我不由天
2楼-- · 2019-08-17 17:59

ok. Found the solution:

$query_args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'meta_query' => array('relation' => 'OR')
);

$i = 1;
while ($i<=5) :
    $i++;
    $query_args['meta_query'][] = array (
        'relation' => 'AND',
        array(
            'key'       => 'foo_' . $i . '_start',
            'compare'   => '>=',
            'value'     => '$start'
        ),
        array(
            'key'       => 'foo_' . $i . '_end',
            'compare'   => '<=',
            'value'     => '$end'
        )
    );
endwhile;
查看更多
登录 后发表回答