1

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 Answers 3

3

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'
    )
  )
);
Sign up to request clarification or add additional context in comments.

Comments

-1

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();

3 Comments

I think, I was unable to explain the situation in details. The requirement is that, if (value of custom field 'cty_name' exists in the given array $cities){ display the post; }
is cty_name your meta_key with multiple values? stored to it?
'cty_name' is just the name of the custom field, multiple values to be compared are stored in $cities array..
-1

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

4 Comments

thank you.. but you just gave an opposite solution.. I want to display all the posts which contain any of the array values (ludhiana, doraha, jagraon..... & all) in a custom field.
now you get the searched value from array put the if condition and match the searced result value to your custom field value if value matches then show the post other wise not
but that would be extremely inefficient at runtime, because i have approx 100 values in the array and thousands of posts to be matched with...
Ok, let me check its efficiency / time taken as I want this to make it as fast possible considering the level of project implementation. Thanks for your support...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.