Support

Account

Home Forums Front-end Issues Filter custom post type by selected custom Taxonomy

Unread

Filter custom post type by selected custom Taxonomy

  • I currently have a Staff directory set up using a custom post type (staff) with a custom taxonomy (area) to divide the staff into separate work areas. It looks something like this:

    SALES
    John Doe
    Jane Doe

    SUPPORT
    Adam Doe
    Ana Doe

    ADMINISTRATION
    Sara Doe
    Steve Doe

    Currently I’ve set up a loop for going through all the work areas and displaying everyone. This is the code I’m using:

    
    <?php
    $custom_terms = get_terms('area');
    
    foreach($custom_terms as $custom_term) {
    	 wp_reset_query();
    	$args = array('post_type' => 'staff',
    	'tax_query' => array(
    	array(
    	'taxonomy' => 'area',
    	'field' => 'slug',
    	'terms' => $custom_term->slug,
    	),
    	),
    	);
    
    $loop = new WP_Query($args);
    if($loop->have_posts()) {
    while($loop->have_posts()) : $loop->the_post();
    
    $name = get_the_title();
    $phone = get_post_meta( get_the_ID(), 'phone', true);
    
    echo '<h5 class="name">' . $name . '</h5>';
    echo '<span class="phone">' . $phone . '</span>';
    
    endwhile;
    
    }
    }
    
    wp_reset_query();
    
    ?>
    

    I would like to improve on this a bit by adding the possibility to check which work areas should be displayed by adding a Taxonomy field. If multiple work areas are selected, I would still like to separate them by work area, like they currently are displayed. Setting up the custom fields is easy but I’m struggling to get anywhere with actually displaying this. I’m guessing I would have to pass the selected areas through the custom terms in some way. Would be very grateful for any ideas!

Viewing 1 post (of 1 total)

The topic ‘Filter custom post type by selected custom Taxonomy’ is closed to new replies.