简体   繁体   中英

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>

As its a custom post type you're dealing with you must specify this in the query by adding 'post_type' => 'artist'

Regarding the categories, if you're using a custom category as well take a look here , you'll need to add something like:

'tax_query' => array(
    array(
        'taxonomy' => 'category',
        'field' => 'id',
        'terms' => array(6,7)
    )
)

If you're using the default WP category you should be able to just use: 'category__in' => array(6,7) instead of category__and

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM