while循环在wp查询数组

时间:2018-06-10 21:34:07

标签: arrays wordpress while-loop

我得到了这个wordpress数组,并且需要在每个'AND'关系中使用while循环:

$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'
        )
    )
)
);

我正在寻找几个小时并试图建立一个功能但没有成功。我该如何解决这个问题?那么“'关系'=&gt;'或',”?

会发生什么
$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;

帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

确定。找到解决方案:

$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;