Support

Account

Home Forums General Issues How do I use tax_query and meta_query together?

Helping

How do I use tax_query and meta_query together?

  • I’ve got a custom post type called ‘lawyer’ with a taxonomy called ‘city_taxonomy’,
    I’m trying to display a list of jobs with a certain tax term [city 1] with (11) , and have acf multi select field his name [service-type] have items (item1, item2 , item3, item4)
    i want to display posts with term 10 and select value item2 ,item3

    <?php
    $args = array(
    ‘post_type’ => ‘lawyer’,
    ‘posts_per_page’ => -1,
    ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘city_taxonomy’,
    ‘field’ => ‘id’,
    ‘terms’ => 11,
    ),
    ),

    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,
    array(
    ‘key’ => ‘service-type’,
    ‘value’ => array(‘item2′,’item3’),
    ‘compare’ => ‘IN’,
    ),
    ),

    );
    print_r($args);
    $loop = new WP_Query($args);
    if ($loop->have_posts()) : ?>
    <?php while ($loop->have_posts()) : $loop->the_post(); ?>
    <?php the_title(); ?>
    <?php endwhile; endif; ?>

  • You would be better off using another custom taxonomy than trying to use a select field. You cannot use “IN” caparisons with a select field. See section 3. Multiple custom field values (array based values) of this document.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.