CharlyAnderson
CharlyAnderson

Reputation: 737

Getting all posts matching custom post type, taxonomy and category

I currently have the following set up:

I have been using the code below to fetch posts within a particular post type:

<?php $loop = new WP_Query( array( 'post_type' => 'workshops', 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

    content here...

<?php endwhile; wp_reset_query(); ?>

My question is: how can I change this to fetch a post from the past-event category within my custom post type, and custom taxonomy?

My aim is to have multiple page templates and target each category individually.

I have tried changing the arrange to target the category alone, but this did not work. I cannot find an online resource on how to target all aspects.

Upvotes: 1

Views: 3859

Answers (2)

Avishay28
Avishay28

Reputation: 2456

If I understand you correctly, you're looking for something like this:

$args = array(
    'post_type' => 'workshops',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'workshop-status',
            'field' => 'slug',
            'terms' => array( 'past-event'),
            'operator' => 'IN'
        ),
    )

);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
    while ( $loop->have_posts() ) : $loop->the_post();

        //do your stuff

    endwhile;
endif;
wp_reset_postdata();

Upvotes: 1

Phill Healey
Phill Healey

Reputation: 3180

You simply need to add the category attribute like so:

$query = new WP_Query( array( 'category_name' => 'past-event' ) );

So in your example case, this would become:

$loop = new WP_Query( array( 'post_type' => 'workshops', 'posts_per_page' => -1, 'category_name' => 'past-event' ) );

You can do a whole load of stuff as detailed in the code

Upvotes: 1

Related Questions