• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

List of post by taxonomy

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › List of post by taxonomy

This topic is: resolved

Tagged: Custom post, loop, taxonomy

  • This topic has 5 replies, 3 voices, and was last updated 10 years, 7 months ago by rgval.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • June 11, 2015 at 4:29 pm #155906
    rgval
    Member

    Hi, I am trying to make a list of students organized by graduation year.
    I created a custom post type called 'student' with a taxonomy 'graduation-year'.

    I have replaced the genesis loop to create my own listing, but somehow it only shows the posts in the first year, the other years are listed but without posts.

    This is the code for my WP archive template: archive-student.php
    (mostly based on http://code.tutsplus.com/tutorials/taxonomy-archives-list-posts-by-taxonomys-terms--cms-20045)

    <?php
    /**
     * STUDENT ARCHIVE
     *
     * @author RGV
     */
    
    // Replace the genesis loop
    remove_action( 'genesis_loop', 'genesis_do_loop' );
    add_action( 'genesis_loop', 'my_custom_loop' );
    
    function my_custom_loop() {
    //start by fetching the terms for the year taxonomy
    	$terms = get_terms( 'graduation-year', array(
    	   'orderby'    => 'name',
    	   'order' => 'DESC'
    	) );
    
    // now run a query of students for each year
    	foreach( $terms as $term ) {
    
    	// Define the query
    	    $args = array(
            	'post_type' => 'student',
    	        'year' => $term->slug
    	    );
     	   $query = new WP_Query( $args );
    
     	  // output the term name in a heading tag
    	    echo'<h2>' . $term->name . '</h2>';
    
     	       // output the post titles in a list
    	       echo '<ul>';
       	     // Start the Loop
    		while ( $query->have_posts() ) : $query->the_post(); ?>
    	         <li class="student-list" id="post-<?php the_ID(); ?>">
       	        	<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        		</li>
                    <?php endwhile;
    	       echo '</ul>';
         // use reset postdata to restore original query
        wp_reset_postdata();
      }
    }
    
    genesis();
    ?>
    

    Any idea what I am doing wrong? Is there a better way of getting this kind of results using genesis?

    Thanks a lot!

    June 12, 2015 at 6:56 am #155974
    Brad Dalton
    Participant

    Try the WordPress Template Hierarchy


    Tutorials for StudioPress Themes.

    June 12, 2015 at 9:06 am #155980
    PatrickODacre
    Member

    This may be obvious, but are you sure you've applied posts to the other years? 🙂

    June 12, 2015 at 12:45 pm #155994
    rgval
    Member

    😀 Yes, good point. I do have post with every taxonomy.
    I confirmed they work with the taxonomy permalink

    June 12, 2015 at 12:48 pm #155995
    rgval
    Member

    Thanks braddalton. What I am trying to do is a template for the custom post archive, so I can list all posts in one page.

    I am surprised is it not more easy to list posts by years...(not the publication year, but a custom numeric field). Apart from my problems with the loop, I though this should a be quite common way of sorting content. I am doing something wrong? perhaps another more efficient structure in the posts, etc.

    June 12, 2015 at 1:54 pm #156010
    rgval
    Member

    I am so sorry, guys, it was a mistake in my taxonomy name.
    I used 'year' as the taxonomy term, and is reserved for WP.

    I am still surprised about how difficult is to list posts grouped by a custom taxonomy in an archive.
    I thought it could be easier with Genesis (perhaps I'm doing something wrong and there is an easier way...)

    Anyway, the working code is here:

    <?php
    /**
     * STUDENT ARCHIVE
     */
    
    // Replace the genesis loop
    remove_action( 'genesis_loop', 'genesis_do_loop' );
    add_action( 'genesis_loop', 'my_custom_loop' );
    
    function my_custom_loop() {
    //start by fetching all terms for the year taxonomy
        $terms = get_terms( 'graduation-year', array(
           'orderby'    => 'name',
           'order' => 'DESC'
        ) );
    
    // now run a query of students for each year
        foreach( $terms as $term ) {
     
        // Define the query
            $args = array(
                'post_type' => 'student',
                'graduation-year' => $term->slug
            );
            $query = new WP_Query( $args );
                
            // output the term name in a heading tag                
            echo'<h2>' . $term->name . '</h2>';
        
            // output the post titles in a list
        echo '<ul>';
        
                // Start the Loop
                while ( $query->have_posts() ) : $query->the_post(); ?>
     
                <li class="student-list" id="post-<?php the_ID(); ?>">
                       <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </li>
                <?php endwhile;
        echo '</ul>';
         // use reset postdata to restore orginal query
        wp_reset_postdata();
      }
    }
    
    genesis();
    ?>
  • Author
    Posts
Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘List of post by taxonomy’ is closed to new replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble