scottiescotsman scottiescotsman - 14 days ago 7
PHP Question

how to list custom post type 'movies' with a genre of 'horror'

I have been trying to list my custom type 'movies' with the custom taxonomy genre with a value of drama.
I have tried several solutions but as yet I have failed.

<?php

$args = array(
'post_type' => 'movies',
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'term_id',
'terms' => 'drama'
)
)
);

$drama = new WP_Query( $args );

if ( $drama->have_posts() ) : while ( $drama->have_posts() ) : $drama->the_post(); ?>


<h4><?php echo the_title(); ?></h4>

<?php endwhile; ?>

<?php else : echo '<p>NO CONTENT FOUND</p>'; ?>

<?php wp_reset_postdata(); ?>

<?php endif; ?>


Hope someone can shed some light on this matter.

Steven

Answer

The field parameter is wrong, in your case you need to set it to slug as drama is not a term_id

$args = array(
   'post_type'     => 'movies',
   'tax_query'     => array(
       array(
         'taxonomy'  => 'genre',
         'field'     => 'slug',
         'terms'     => 'drama'
      )
   )

);

Hope it works !