• Hey guys,
    I’ve added a People custom post type and am adding departments using custom taxonomies.
    For the People page, I would like it to display a specific term from the taxonomy as a title and follow with the People posts that are related to this taxonomy term.
    Then it should display the next taxonomy term with the People posts listed etc. repeating until end of the taxonomy term list.

    This code from the Codex displays the taxonomy terms no problem

    $terms = get_terms("departments");
     $count = count($terms);
     if ( $count > 0 ){
         echo "<ul>";
         foreach ( $terms as $term ) {
           echo "<li>" . $term->name . "</li>";
         }
         echo "</ul>";

    and this code displays what I need from the posts

    $loop = new WP_Query(array('post_type' => 'people', 'posts_per_page' => 10, 'orderby' => 'menu_order', 'order' => 'ASC'));
            while ($loop->have_posts()) : $loop->the_post(); ?>
                <h2><a>"><?php the_title(); ?></a></h2>
                <?php
                if (has_post_thumbnail()) { // check if the post has a Post Thumbnail assigned to it.
                    ?><a>"><?php the_post_thumbnail(); ?></a><?php
    
                }
                the_excerpt();
                endwhile;

    So what’s the best way of accomplishing what I need? I’m certain that the post loop will have to nest inside the taxonomy loop so that the posts can be queried based upon the current taxonomy term but I’m unsure how to achieve this.

    Thanks in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Try this:

    $args=array(
      'post_type' => 'people',
      'post_status' => 'publish',
      'posts_per_page' => 10,
      'tax_query' => array(
    		array(
    			'taxonomy' => 'departments'
    		)
    );
    
    $my_query = null;
    $my_query = new WP_Query($args);
    
    // The Loop
    while ( $my_query->have_posts() ) : $my_query->the_post();
    
    	echo '<li>';
    
            echo '<h4>';
            //get term title
            echo '</h4>';
    
    	the_title();
    
    	echo '</li>';
    
    endwhile;
    // Reset Post Data
    wp_reset_postdata();
    Thread Starter digitaldeath

    (@digitaldeath)

    Thanks.
    Here’s my implemented solution:

    // Get the names (terms) of all departments
        $terms = get_terms("departments");
    
        // How many departments?
        $count = count($terms);
    
        if ($count > 0) {
            $div_counter = 0;
    
            // For each department, do the following
            foreach ($terms as $term) {
    
                // We don't want to generate duplicate output for certain departments, so we filter those out.
                if (($term->slug != 'executive-board') && ($term->slug != 'scientific-and-technical-board') &&
                    ($term->slug != 'managers'))
                {
    
                    // Determine if this section should be placed in the left or right column
    
                    $div_counter += 1;
                    if (($div_counter % 2) == 1) { // Odd number - right column
                        ?><div class="people-right-column"><?php
    
                    } else {
                        ?><div class="people-left-column"><?php  // Even number - left column
                    }
    
                    // Print the department name
                    echo "<h4 class=\"dept-title\">" . $term->name . "</h4>";
    
                    // Check to see what posts are tagged with the current department name (term) - the current department name is known as we're inside the loop here.
                    $args = array('post_type' => 'people', 'taxonomy' => 'departments', 'term' => $term->name, 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC');
                    $loop = new WP_query($args);
    
                    // While there are posts within the current department term, do something with the post (display required information etc.)
                    while ($loop->have_posts()) : $loop->the_post();
                        ?>
                        <div class="profile-preview">
    
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> (<?php the_field('title'); ?>)
                            <?php
                                 the_excerpt();
                            ?>
                        </div>
                            <?php endwhile; ?></div><?php
                }
            }
        }
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom post type and taxonomy problem’ is closed to new replies.