0

I have a custom Post Type called Movies, and an associated Taxonomy called Genre. Now I need to list all CPT(s) which are in Genre of 'Comedy' in my own custom page (regardless of WP parmalink mechanism)

Following code only returns the custom post type moviesCPT:

$args = array( 'post_type' => 'moviesCPT', 'posts_per_page' => 10 );
$loop = new      WP_Query( $args ); 
while ( $loop->have_posts() ) :
 $loop->the_post(); the_title(); 
echo '<div class="entry-content">'; the_content();
echo '</div>'; 
endwhile;

Can you please let me know how to handle this?

2

Specify this when you make your query:

'genre' => 'comedy'

Where genre is the name of your taxonomy ( not the human readable version, but the internal slug ), and comedy is the slug of the term you want to filter by

Importantly, indent your query parameters, and put them each on their own line so that it's easy to read/type/change, a good editor will do this for you. You also need to do an if( $loop->have_posts() ) to check that you got posts and handle things if none were found, and a wp_reset_postdata call to clean up at the end ( otherwise any calls you make will show the last movie in the loop, rather than the current post/page )

You can use the tax_query parameter for more complex queries, the details are in the codex under the WP_Query documentation

|improve this answer|||||
  • I will start to love these taxonomies. – prosti Jan 13 '17 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.