WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How can I filter custom post type taxonomy results? (5 posts)

  1. w1cky
    Member
    Posted 1 year ago #

    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.

    Example:

    The user clicks London
    (on mysite.com/courses-overview)

    And is shown 3 tables: Design; Photography; Drawing
    (on mysite.com/course-location/london)

    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>		
    
    <?php
    	$args = array(
    		'post_type' => 'courses',
    		'course_subject' => 'design',
    		'posts_per_page' => 200
    	);
    	$currentDesignCourses = new WP_Query( $args );
    ?>
    
    <table>
    ... stuff ...
    </table>
  2. w1cky
    Member
    Posted 1 year ago #

    Never mind. I found the solution.

  3. villagechris
    Member
    Posted 1 year ago #

    Curious - what was the solution?

  4. w1cky
    Member
    Posted 1 year ago #

    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.

  5. w1cky
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic