-1

I want print array in a loop and want to put it inside WP_Query.

Is it Possible? If there are other alternatives kindly mention it.

    $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'
    ),*/
)));
  • You are asking a pure PHP question, really: "How do I create an array dynamically?" Its not inherently WordPress. One example of doing that is here: wordpress.stackexchange.com/a/142490/21376 – s_ha_dum Jan 1 '16 at 16:16
  • Please merge your accounts and stick with one only – Pieter Goosen Jan 1 '16 at 19:00
1

Just construct what you need outside of the main query.

// 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,
    ));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.