March 15, 2016
Drilling down across multiple post types can help enhance relationships between the content on your website, and direct the flow of traffic on your website.
However, working with more than one loop can be tricky. Thanks to this WordPress Support post, the breakdown was simple for me to figure out, please see the original post here, as well as copied code below.
First loop:
<?php $args = array( 'taxonomy' => 'fan_post_types', 'term' => 'fan-comments', 'post_type' => 'fan', 'posts_per_page' => 8 ); $comments = new WP_Query($args); if($comments->have_posts()) : while($comments->have_posts()) : $comments->the_post(); ?> Whatever you want to display goes here... <?php endwhile; endif; ?> <?php wp_reset_query(); // end Fan Cooments loop. ?>
Second Loop:
<?php $args = array( 'taxonomy' => 'fan_post_types', 'term' => 'fan-merchandise', 'post_type' => 'fan', 'posts_per_page' => 8 ); $merchandise = new WP_Query($args); if($merchandise->have_posts()) : while($merchandise->have_posts()) : $merchandise->the_post(); ?> Whatever you want to display goes here... <?php endwhile; endif; ?> <?php wp_reset_query(); // end Fans Merchandise loop. ?>
Inserting both loops into the template:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php the_content(); ?> Loop 1 Loop 2 <?php endwhile; endif; ?>
Tying it all together example:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php $args=array( 'post_type' => 'restaurant', 'posts_per_page' => -1 ); $comments = new WP_Query($args); if($comments->have_posts()) : while($comments->have_posts()) : $comments->the_post(); ?> <?php the_post_thumbnail(); ?> <?php the_content(); ?> <?php endwhile; endif; ?> <?php wp_reset_query(); // end Fan Cooments loop. ?> <ul> <?php $terms = get_the_terms( $tours->ID, 'stops'); foreach($terms as $term) {} $args=array( 'tax_query' => array(array( 'taxonomy' => 'stops', 'field' => 'term_id', 'terms' => array($term->term_id) )), 'post_type' => 'tour', 'posts_per_page' => -1, 'post_status' => 'all' ); $merchandise = new WP_Query($args); if($merchandise->have_posts()) : while($merchandise->have_posts()) : $merchandise->the_post(); ?> <li> <h2 style="background: <?php the_field('tour_color', $term); ?>"> <a href="<?php the_permalink(); ?>"><?php the_date('l, F d, Y'); ?></a> </h2> </li> <?php endwhile; endif; ?> </ul> <?php wp_reset_query(); ?> <?php endwhile; endif; ?>