Loop for custom post types filtered by a taxonomy
I am using the Custom Post Type feature in WordPress 3 to create an Events section for my website. I am able to get this to display by using the relevante template files, but I also want the event listings visible on other pages.
I’ve done this with the normal post types but these are based on category IDs which Custom Post Types don’t have.
Each event has a taxonomy for country and I want to be able to loop through the events and display only events for specific countries so I want to be able to filter that.
I’ve had a look on the Codex and came up with the following but it doesn’t display anything:
<?php $args = array(
'post_type'=> 'events',
'country' => 'england'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
?>
Leave an answer