1 query with multiple terms displayed differently?
-
I have a custom post type w/ a custom taxonomy. I have a page where I display one post from each of the terms. The content that’s displayed for each term is styled differently, sometime it’s just the featured image I need, sometimes a bunch of custom meta, etc.
Right now I’m using a query for each, resetting, then doing another. It works perfectly, but it seems like there must be a more efficient way to do that.
Here’s what I’m doing now. The only thing that changes from query to query is the taxonomy term and what information I need:
<?php $customQuery = array( 'post_type' => 'cpt_name', 'posts_per_page' => '1', 'order' => 'ASC', 'tax_query' => array( array( 'taxonomy' => 'custom_tax', 'field' => 'slug', 'terms' => 'term#1' ) ) ); $custom = new WP_Query( $customQuery ); ?> <?php if ($custom->have_posts()) : ?> <?php while ( $custom->have_posts()) : $custom->the_post(); ?> [title, content, etc.] <?php endwhile; ?> <?php endif; ?> <?php wp_reset_query(); ?> <?php $customQuery = array( 'post_type' => 'cpt_name', 'posts_per_page' => '1', 'order' => 'ASC', 'tax_query' => array( array( 'taxonomy' => 'custom_tax', 'field' => 'slug', 'terms' => 'term#2' ) ) ); $custom = new WP_Query( $customQuery ); ?> <?php if ($custom->have_posts()) : ?> <?php while ( $custom->have_posts()) : $custom->the_post(); ?> [thumbnail, title, excerpt, custom meta, etc.] <?php endwhile; ?> <?php endif; ?> <?php wp_reset_query(); ?>
- The topic ‘1 query with multiple terms displayed differently?’ is closed to new replies.