如何在WP_QUERY内创建循环?

时间:2016-01-01 作者:Irfan

我想把数组打印成一个循环,然后把它放进去WP_Query.

有可能吗?如果有其他选择,请提出来。

    $wp_user_query = new WP_User_Query(array(\'role\' => \'Subscriber\', \'fields\' => \'all\',
\'meta_query\' => array(
    \'relation\' => \'OR\',
    // Here I want to create a loop
    // loop will start from here repeating the array
    array(
    \'key\'       => \'user_group_name\',
    \'value\'     => \'Panera Group 2 Encino\',
    \'compare\'   => \'LIKE\'
    ),
    // loop end
    /*array(
    \'key\'       => \'user_group_name\',
    \'value\'     => \'West Hollywood\',
    \'compare\'   => \'LIKE\'
    ),*/
)));

1 个回复
SO网友:jgraup

只需在主查询之外构造所需的内容。

// terms to search
$meta_query_keys = array(\'Panera Group 2 Encino\', \'West Hollywood\');

// start the meta query off
$meta_query = array(\'relation\' => \'OR\');

// add terms to query
foreach($meta_query_keys as $key) {
    $meta_query[] = array(
        \'key\'     => \'user_group_name\',
        \'value\'   => $key,
        \'compare\' => \'LIKE\',
    );
}

// construct main query
$wp_user_query = new WP_User_Query(
    array(\'role\'       => \'Subscriber\',
          \'fields\'     => \'all\',
          \'meta_query\' => $meta_query,
    ));