Archive for custom taxonomy lists all posts instead of current taxonomy

I'm using the Types plugin for a custom post type "Session", which has the custom taxonomy "Semester". I would now like to create an archive page for each semester which is automatically generated. Creating a taxonomy-semester.php was no problem, however, that page always shows contents for ALL semesters, not just the current one the URL points to.

E.g. /semester/winter2015 and /semester/summer2016 will both show contents for Winter 2015 (or whatever is in the database). I'm pretty sure the problem lies within my query arguments, but no matter what I try, I can't get this page to show only the contents for the correct semester. Here's my code:

?php get_header(); ?
section class="index-post-list"     

?php 

$term = $wp_query-queried_object;
$getterm = $term-slug; // get current slug (E.g. winter2015)

    $args = (array(
    'post_type' = 'session', 
    'tax_query' = array(                     
        'taxonomy' = 'semester',
        'field' = 'slug',
        'terms' = $getterm,
        'include_children' = true,          
        'operator' = 'IN' 
    ),
    'meta_key' = 'wpcf-start-time', // custom post field by which results are sorted
    'orderby' = 'meta_value',
    'order' = 'ASC'
    ) );  

    $query = new wp_query( $args );

        h2 class="archive-title"?php printf( __( 'Schedule for %s', 'template' ), single_tag_title( '', false ) ); ?/h2
        ?php echo tag_description(); ?


        ?php if ( $query - have_posts() ) : while ( $query - have_posts() ) : $query - the_post(); ?


    //usual loop stuff goes here....



    ?php endif; ?



/section!--index-post-list--
?php get_footer(); ?

Topic plugin-types wp-query custom-taxonomy query-posts custom-post-types Wordpress

Category Web


You have a syntax error in your query. According to the documentation for WP_Query, tax_query is an array of arrays of parameters, which is to say that it should probably look like this:

'tax_query' => array(
    array(                     
        'taxonomy' => 'semester',
        'field' => 'slug',
        'terms' => $getterm,
        'include_children' => true,          
        'operator' => 'IN' 
    ),
),

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.