1
votes

I have an array of city names

$cities = array('Ludhiana','Doraha','Jagraon','Moga','Phillaur','Ahmedgarh');

In my wordpress implementation, I have a custom-field named 'cty_name'

Now, I want to query those posts which contains any array value in the 'cty_name' custom field. I know this could be done using meta_query in Wp_Query function, but could not figure this out.

Please lead me to a fast and efficient way to get these posts, actually I would be having approximately 100 values in the array, and that would be inefficient to compare each value with the posts.

Thank You,

EDIT :

I solved the Problem, it was an easy one though... Thanx all for your kind support

$cities = array('Ludhiana','Doraha','Jagraon','Moga','Phillaur','Ahmedgarh');

$args = array(
'post_type' => 'city_posts',
'meta_query' => array(
    array(
        'key' => 'cty_name',
        'value' => $cities,
        'compare' => 'IN'
    )
  )
);
3

3 Answers

3
votes

I solved the Problem, it was an easy one though... Thanx all for your kind support

$cities = array('Ludhiana','Doraha','Jagraon','Moga','Phillaur','Ahmedgarh');

$args = array(
'post_type' => 'city_posts',
'meta_query' => array(
    array(
        'key' => 'cty_name',
        'value' => $cities,
        'compare' => 'IN'
    )
  )
);
-1
votes

Try this for size with wp_query, im going to infer that your key is cty_name:

$args = ( 'meta_key' => 'cty_name'); // this will get all posts with meta key cty_name
$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    echo '<li>' . get_the_title() . '</li>';
    the_content();
endwhile;


wp_reset_postdata();
-1
votes

Try to use this one

<?php $punjab= array ('Ludhiana','Doraha','Jagraon','Moga','Phillaur','Ahmedgarh'); print_r($punjab); $pos = array_search('Ludhiana', $punjab); echo 'Phillaur found at: '.$pos; ?>