1

Thanks in advance for all your help!

I want to display posts (tours) that are in a specific location (city) so in my location_taxonomy.php I have a taxonomy query in the form of:

$args = array(
    'post_type'     => 'tour',
    'meta_key'      => 'trav_tour_city',
    'meta_value'    => $term->term_id, //city id
);

When the key stored in the tour has only one value (one city), it displays the posts. When the key has more than one value (multiple cities or id separated by commas), it doesn't.

How can I change the $args so the query would determine that the current city ($term->term_id) exists within the comma separated values stored in 'trav_tour_city'?

I have tried something in the line of:

                 // trying multiple cities 
                $args = array(
                    array(
                    'post_type'     => 'tour',
                    'meta_key'      => 'trav_tour_city',
                        'meta_query' => array(
                            array(
                                'value'   => $term->term_id, 
                                'compare' => 'IN',
                                ),
                        ),
                    ),
                );  

but obviously I don't know what I'm doing... haha..

Thanks guys...

0

I found my answer in another forum...

This works:

              $args = array (
                    'post_type' => 'tour',
                    'meta_query'=> array(
                        array(
                            'key' => 'trav_tour_city', 
                            'compare' => 'LIKE',
                            'value' => $term->term_id,
                        )
                    ),
                    'meta_key' => 'trav_tour_city',
                );
0

If your field is saved in a string format you can try using LIKE, like the example below:

 // trying multiple cities 
    $args = array(
      array(
        'post_type'   => 'tour',
         'meta_query' => array(
          array(
            'meta_key'=> 'trav_tour_city',
            'value'   => (string) $term->term_id, 
            'compare' => 'LIKE',
          ),
        ),
    );  
  • It didn't work though... but why if I use this, it works? $args = array( 'post_type' => 'tour', 'meta_key' => 'trav_tour_city', 'meta_value' => '87,79', 'compare' => 'LIKE', ); – Ivan Lemus Aug 1 '17 at 20:46
  • In other words... 'meta_value' => '87,79' is reading the whole string stored in the value field... is there a way to scan that field and just find '79' within the string? – Ivan Lemus Aug 1 '17 at 20:47
  • I updated my answer, can you try it again ? – Cesar Henrique Damascena Aug 1 '17 at 21:24

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.