1

The the code below pulls the 'plan type' taxonomy from the custom post type 'floorplan' and displays the posts accordingly, that works great. The problem I'm having is that it works only when each post has only one taxonomy. When I add multiple taxonomies to a custom post, the taxonomy filter no longer works.

For example, the homes have plan types of < 1500 square feet, > 1500 square feet, > 2500 square feet, and all plans. As it stands, it works properly if using only one of those taxonomies, but, I'd like to be able to apply the taxonomy all plans to each floor plan so that when filtered, I can see every post.

<?php

  $terms = wp_get_post_terms( $post->ID, 'plan type' );
  if($terms){
    // post has course_type terms attached
    $course_terms = array();
    foreach ($terms as $term){
    $course_terms[] = $term->slug;
  }

  $original_query = $wp_query;
  $wp_query = null;
  $wp_query = new WP_Query( array(
   'post_type' => 'floorplans',
   'tax_query' => array(
      array(
       'taxonomy' => 'plan type',
       'field' => 'slug',
       'terms' => $course_terms, //the taxonomy terms I'd like to dynamically query
        'posts_per_page' => '-1',
       ),
      ),
        'orderby' => 'title',
        'order' => 'ASC',
        'posts_per_page' => '-1',
      ));

  if ( have_posts() ): ?>
      <ul class="gallery_list cf">
     <?php
       while (have_posts() ) : the_post();
       $thumb = get_field('floorThumb');
     ?>
     <li>
       <a href="<?php the_permalink(); ?>">
         <div class="gallery_thumb">
            <img src="<?php echo $thumb['url']; ?>" alt="<?php echo $thumb['alt']; ?>">
         </div>
         <div class="overlay"></div>
         <h2><?php the_title(); ?></h2>
       </a>
    </li>
    <?php endwhile; ?>
    </ul>
  <?php endif;
      $wp_query = null;
      $wp_query = $original_query;
      wp_reset_postdata();
  } // end if($terms)
} ?>
  • I'm not sure I understand your issue. Your code appears to get terms from a post in the main query, then query for other floorplans with terms that match that post? As far as the all floorplans term is concerned, don't add a tax query if you want all of the floorplans returned. – Milo Feb 7 '16 at 1:39
  • Where and how the $course_terms is defined? Is it something being posted as search term? – Abhik Feb 7 '16 at 6:34
0

You are using WP_query, try to change your loop to:

<?php
if ($wp_query->have_posts() ):
     while ($wp_query->have_posts() ) : $wp_query->the_post();
    // Do stuff here
    endwhile; 
endif;
wp_reset_postdata();
?>

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.