Support » Themes and Templates » How can I filter custom post type taxonomy results?

  • Resolved w1cky


    I’m sure that this has been covered but I can’t find the solution anywhere. Please can somebody help?

    I have a custom post type of Courses, which has 2 taxonomies: Course Subject and Course Location.

    I have set up a page entitled Courses Overview, with some helpful intro text. Beneath this text I have displayed links to the Course Locations:

    <?php $args = array (
    	'taxonomy' => 'course_location',
    	'title_li' => ''
    ) ; ?>
    <?php wp_list_categories( $args ); ?>

    Each Course Subject is displayed in a separate table on the taxonomy-course_location.php template.


    The user clicks London

    And is shown 3 tables: Design; Photography; Drawing

    Everything is working correctly except I can’t work out how to filter the results. So, using the example above, The Design table shows ALL of the design courses, not just the ones in London.

    How do I restrict the results to only be for the current Location?

    This is my code for the taxonomy-course_location.php template:

    <?php $term =	$wp_query->queried_object; ?>
    <h1>Courses in: <?php echo $term->name ; ?></h1>		
    	$args = array(
    		'post_type' => 'courses',
    		'course_subject' => 'design',
    		'posts_per_page' => 200
    	$currentDesignCourses = new WP_Query( $args );
    ... stuff ...
Viewing 4 replies - 1 through 4 (of 4 total)
  • Never mind. I found the solution.

    Curious – what was the solution?

    After clicking through from the taxonomy term on the previous page (eg. View courses in London), the relevant info is shown in a table on a subsequent page. You need to query the object which contains the name, slug, etc.

    Here a stripped down version of the code:

    <?php $locationTerm = $wp_query->queried_object->slug; 
    $args = array(
    	'course_location' => $locationTerm,
    	'course_type' => 'design',
    	'post_type' => 'courses',
    	'orderby' => ' . $startDate . ',
    	'order' => 'ASC',
    	'posts_per_page' => 2000
    $currentCourses = new WP_Query( $args );
    <?php if($currentCourses->post_count != 0 ) :  while ($$currentCourses->have_posts()) : $currentCourses->the_post(); ?>
    … DO STUFF HERE … 
    <?php endwhile; endif; ?>

    The post type is Courses. Course Location and Course Type are taxonomies.

    It’s worth mentioning that I’ve had issues with ordering via the menu_order argument, so I’ve started using get_posts and a foreach loop instead of WP_Query for custom post types.

    It seems to work much more consistently.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How can I filter custom post type taxonomy results?’ is closed to new replies.