Tristan
Tristan

Reputation: 17

How do I loop posts for a custom category for a custom post type in WordPress?

The code I have below loops all posts within one of my custom post types. I need to loop for a specific category within the custom post type.

<?php 
    $query = new WP_Query( array( 'post_type' => 'case-study' ) ); 
    if ( $query->have_posts() ) : 
        while ( $query->have_posts() ) : $query->the_post(); 

            get_template_part( 'template-parts/content', 'work' ); 

         endwhile;
    endif; 
?>

What do I need to change for it it loop a specified category within the custom post type?

Upvotes: 0

Views: 4610

Answers (1)

PhpDude
PhpDude

Reputation: 1600

You can try the following:

$query = new WP_Query( array(
    'post_type' => 'case-study',          // name of post type.
    'tax_query' => array(
        array(
            'taxonomy' => 'category',   // taxonomy name
            'field' => 'term_id',           // term_id, slug or name
            'terms' => 48,                  // term id, term slug or term name
        )
    )
) );

Upvotes: 2

Related Questions