1

I'm trying to use one query for two queries, one with an and relation and one without without having to do multiple queries and loops, is this possible?

 <?php 
$args = array(
    'post_type' => 'local_service',
    'posts_per_page' => -1, 
    'tax_query' => array(
        array(
            'taxonomy' => 'local_area',
            'field'    => 'name',
            'terms'    => array( $localarea ),
        ),
        'relation' => 'AND',
        array(
            'taxonomy' => 'service_type',
            'field'    => 'name',
            'terms'    => array( $catagory ),
        ),
        array(
            'taxonomy' => 'local_area',
            'field'    => 'name',
            'terms'    => array( $localarea ),
        ),
    ),
);

$loop = new WP_Query( $args );
?>

<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <article>
        <div class="panel tvh-block-primary search-result">
            <div class="panel-heading">  
                <?php the_title( '<h3 class="panel-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">','</a></h3>' ); ?>
            </div>
            <div class="panel-body">
                <p><?php the_excerpt(); ?></p>
            </div>
        </div>
    </article>
    <?php  wp_reset_postdata(); ?>
<?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
  • I've formatted your code for readability. What posts do you want to retrieve, exactly? – engelen Apr 1 '16 at 10:04
  • I have a search results page where I want to get either local services by just using the local area or local services using both the service type and the local area without having multiple queries. – Emmie Apr 1 '16 at 10:25
  • And how do you determine which query to use? – engelen Apr 1 '16 at 11:26
  • I use Input box and a dropdown and check if it has been set like this: '$postcode = $_GET['enter-postcode']; $catagory = $_GET["st"]; if(isset($_GET['cat'])){ $localarea = $_GET['cat']; } else{ $localarea = $_COOKIE['local_area']; } ' – Emmie Apr 1 '16 at 13:19

Your Answer

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

Browse other questions tagged or ask your own question.