1
votes

I currently have the following set up:

  • Custom post type called "workshops"
  • Within the workshop post type I have a taxonomy called "workshop-status"
  • Workshop status subsequently has categories for example "past-event"

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.

2
Use the below. You can pass a category id, a list of ids, the category slug, etc. codex.wordpress.org/Class_Reference/…pendo
You can use the tax_query argument in order to fetch posts from several taxonomies. codex.wordpress.org/Class_Reference/…Avishay28

2 Answers

1
votes

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

1
votes

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();