Support

Account

Home Forums Backend Issues (wp-admin) Multiple Categories Loop (Taxonomy)

Helping

Multiple Categories Loop (Taxonomy)

  • Hi there,

    I have 50 items that have to be sorted using custom categories (15 of those). I’m using ACF and CPT UI for this. My custom post type name is “cars” and custom taxonomy is “car_category”. Many of the items will be assigned to multiple categories. I used checkbox for this. When I started adding those multiple categories to actual items they showed up completely mixed up. I can see only recently uploaded items no matter which category I choose to view. I know I’m missing something but I can’t find my mistake.

    Here’s my code from taxonomy-car_category.php:

      <?php 
    
      $posts = get_posts(array(
      'posts_per_page'  => -1,
      'post_type'           => 'cars'
      ));
    
      if( $posts ): ?>
    
        <?php while ( have_posts() ) : the_post(); ?>
    
          <a href="<?php the_permalink(); ?>">
            <?php echo the_post_thumbnail(''); ?>
          </a>
    
        <?php endwhile; ?>
    
      <?php endif; ?>

    Thank you!

  • I rewrote my code to use WP_Query but unfortunately this method returns all posts instead of posts assigned to a specific category.

    <?php
    
    $args = array(
        'post_type'      => 'cars',
        'posts_per_page' => -1,
        'taxonomy' => 'car_category',
    
    );
    
    $the_query = new WP_Query( $args ); ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
        <?php while ( $the_query->have_posts() ) : $the_query->the_post();?>
    
          <a href="<?php the_permalink(); ?>">
            <?php echo the_post_thumbnail(''); ?>
          </a>
    
        <?php endwhile; ?>
    
    <?php else : ?>
    
    <p>Sorry, no products found.</p>
    
    <?php endif; ?>
    
    <?php wp_reset_postdata() ?>
    
    <?php wp_reset_query() ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Multiple Categories Loop (Taxonomy)’ is closed to new replies.