Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Sebastian Nadeau
28,625 PointsHow do you properly filter out a custom post type with unique taxonomy categories in WordPress?
I am building a WordPress website where I am trying to build a product listing page that can be organized by categories. I am not using the build in categories in WordPress, I am using a custom taxonomy named "Products" to filter out the products listing page.
I created custom post types for each brand of the products using the plugin Custom Post Type UI as well as Advanced Custom Fields. I set the parameter "Has Archive" to true and in the menu options I set add support to the "Products" taxonomy.
In addition to the brand custom post types, I added "In Store Specials" and "New Arrivals".
I am setting up the product listing page in a taxonomy.php file where I am using the WP_Query class to loop through each individual custom post type. Within this loop I am using the tax-query parameter to gather all the posts for each specific custom post type where I am trying to filter out the posts by category respectively.
The issue I am running into is that when I assign one product to one specific category, it appears in the other categories that another product was assigned to. So when I view one category I get two products posts, even though only one product was assigned to that category.
I did my best to describe the problem I am having and would appreciate feedback to help me resolve this issue. Here is the code I am working with in the taxonomy.php file. I would really appreciate expert feedback. Thank you.
<?php
$terms = get_terms( 'products' );
$args = array(
'post_type' => 'in_store_specials',
'tax-query' => array (
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $terms,
'include_children' => true,
'operator' => 'IN'
)
)
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-xs-12 col-sm-6 col-md-4 product filterable dynavector">
<div class="portfolio-content">
<div class="img-overlay">
<?php the_post_thumbnail('medium'); ?>
<a class="hover-plus" href="<?php the_permalink(); ?>">
<i class="fa fa-plus" aria-hidden="true"></i>
</a>
</div>
<h4 class="product-title"><?php the_title(); ?></h4>
<btn class="btn btn-product">
<i class="product-icon ion-ios-redo"></i><a href="<?php the_permalink(); ?>">View Product</a>
</btn>
</div>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
<?php
$terms = get_terms( 'products' );
$args = array(
'post_type' => 'new_arrivals',
'tax-query' => array (
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $terms,
'include_children' => true,
'operator' => 'IN'
)
)
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-xs-12 col-sm-6 col-md-4 product filterable dynavector">
<div class="portfolio-content">
<div class="img-overlay">
<?php the_post_thumbnail('medium'); ?>
<a class="hover-plus" href="<?php the_permalink(); ?>">
<i class="fa fa-plus" aria-hidden="true"></i>
</a>
</div>
<h4 class="product-title"><?php the_title(); ?></h4>
<btn class="btn btn-product">
<i class="product-icon ion-ios-redo"></i><a href="<?php the_permalink(); ?>">View Product</a>
</btn>
</div>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>