Search code examples
phpwordpressloopscategoriestaxonomy

Show posts from 2 categories/taxonomys in custom post type


I'm making the line-up page of a festival website in wordpress. I've added a custom post type in my wordpress theme "Artists". I have 3 categories that represent different days and 2 categories that represent the different stages each festival day. I now want to show all artists sorted by day and by stage. For example:

Friday
ROOM1: ARTIST1, ARTIST2, ARTIST3, ...
ROOM2: ARTIST4, ARTIST5, ARTIST6, ...
SATURDAY
ROOM1: ARTIST1, ARTIST2, ARTIST3, ...
ROOM2: ARTIST4, ARTIST5, ARTIST6, ...
SUNDAY
ROOM1: ARTIST1, ARTIST2, ARTIST3, ...
ROOM2: ARTIST4, ARTIST5, ARTIST6, ...


I tried for hours finding the right code for this but no cigar, is the use of "category" wrong here because it's a custom post type?

Thanks for your help

<h2>FRIDAY</h2>
                        <?php // the loop ?>
                            <?php $query = new WP_Query( array( 'category__and' => array( 6, 7 ) ) ); ?>

                            <?php if ($query->have_posts()) : ?>

                                <?php while ($query->have_posts()) :$query->the_post(); ?>

                                    <?php $query->get_template_part( 'includes/loop' , 'index'); ?>

                                <?php endwhile; ?>                  

                            <?php else : ?>

                                <p><?php _e( 'Sorry, no artists found.', 'themify' ); ?></p>

                            <?php endif; ?> 


                        <h2>SATURDAY</h2>

                        <h2>SUNDAY</h2>

Solution

  • You should try something like this:

    <?php $query = new WP_Query( 
                       array(
                           'tax_query' => array(
                                array(      
                                    'taxonomy' => '[taxonomy-name]',
                                    'field' => 'id',
                                    'terms' => array(6, 7),
                                    'operator' => 'AND'
                                )
                            ),
                            'posts_per_page' => -1
                        )
                    ); ?>
    
    <?php if ($query->have_posts()) : ?>
        <?php while ($query->have_posts()) :$query->the_post(); ?>
            <?php $query->get_template_part( 'includes/loop' , 'index'); ?>
        <?php endwhile; ?>                  
    <?php else : ?>
       <p><?php _e( 'Sorry, no artists found.', 'themify' ); ?></p>
    <?php endif; ?>
    

    Don't forget to fill in the right taxonomy-name and you can just add terms to the array Hope this helps.