Loop for a custom post type doesn’t retrieve posts from some categories
-
Hi Everyone,
I have a strange problem – I have a custom post type ‘products’. Under it there are several categories, let’s say chairs, tables etc. I wanted to filter the results – when I choose a category to see the results grouped by brand and vice versa when I choose a brand to have the products from different categories grouped together.
I managed to do the first by creating different loops for every brand. However, I can’t implement the same strategy for the categories, because for some reasons not every category retrieves the posts.
I.e. if I choose chairs, they will show, but if I choose tables for the loop, they won’t. I tried filtering them by term_ID, slug etc. but it doesn’t matter. They are showing okay when I filter them by brand. I am using a simple loop no idea why it’s not working. Here is the code:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <?php $postType = get_post_type( $post ); if ($postType == 'product') { ?> <?php $terms = get_the_terms( $post->ID , 'brand' ); $categories = get_the_terms( $post->ID , 'category' ); ?> <?php foreach ($categories as $category) { $category = $category->term_id;} foreach ( $terms as $term ) {$term = '<div class="product-brand">'. $term->name. '</div>'; }?> <?php if (($category == '33')){ ?> <?php if ( has_post_thumbnail() ) {the_post_thumbnail('ws-post-thumb');} ?> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a> <br/><?php echo $term ?></h3> <?php }} ?> <?php endwhile; ?> <?php else : ?> <?php endif; ?>
I’ve been stuck for the past several days trying to find a solution. Thanks.
- The topic ‘Loop for a custom post type doesn’t retrieve posts from some categories’ is closed to new replies.