• I have a custom post type called heartspace-podcasts, that has custom fields and a custom taxonomy called “podcast-season” – Season 1, Season 2, etc.

    I am looking for help to modify the loop on my custom archive page to group the podcasts by season. How would I modify this loop?

    <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>
            <div class="col-md-3">
                 <a href="<?php the_permalink() ?>"><div class="podcast-thumb"><img src="<?php the_field('episode_image'); ?>" class="aligncenter" style="margin-bottom:10px;"></div></a>
                   <h3 style="color:#f28fad; font-size:20px; margin-top:10px; "><a href="<?php the_permalink() ?>" >
                <?php the_field('podcast_episode_title'); ?></a></h3>
         <?php echo strip_tags( get_the_excerpt() ); ?>
              <div class="clearfix"></div>
            </div>
            <?php endwhile; ?>
            <?php else : ?>
            <h1>404 | Nothing Found</h1>
            <p>We're sorry, but the page you're looking for cannot be found. Please return to our <a href="<?php echo get_option('home'); ?>">homepage</a> or try again.</p>
            <?php endif; ?>
            <div class="pagination">
              <?php
              global $wp_query;
    		  $big = 999999999;
    		  echo paginate_links( array(
    			'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    			'format' => '?paged=%#%',
    			'current' => max( 1, get_query_var('paged') ),
    			'total' => $wp_query->max_num_pages
    			) );
    	  ?>

    Thanks for any help you can offer.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The loop doesn’t have anything to do with grouping or ordering of posts. It simply outputs the posts as they are ordered by the query. Any ordering or re-ordering needs to occur before the loop starts.

    The query used by the loop is saved in the global $wp_query. The posts are listed in $wp_query->posts. You could use PHP’s usort() function to rearrange the posts by taxonomy term. This is not very efficient though, sorting with the SQL query would be much more efficient. Unfortunately, the WP_Query class does not offer an order by taxonomy term argument. It will order by custom fields though. Custom fields as an organizational device might be an option if the taxonomy schema is not already set in stone.

    To order query results by taxonomy term name, you will need to write your own SQL query. If ordering by term ID would work for you, the query is simpler than ordering by name. An example query that orders posts by taxonomy slug name follows. You could adapt it to your situation. The loop will need adjustments to work with $wpdb results.

    global $wpdb;
    $query = $wpdb->get_results( "SELECT * FROM $wpdb->posts AS p
    	LEFT JOIN $wpdb->term_relationships AS r ON (p.ID = r.object_id)
    	INNER JOIN $wpdb->term_taxonomy AS x ON (r.term_taxonomy_id = x.term_taxonomy_id)
    	INNER JOIN $wpdb->terms AS t ON (r.term_taxonomy_id = t.term_id)
    	WHERE p.post_type IN ('post', 'portfolio') 
    	AND p.post_status = 'publish'
    	AND x.taxonomy = 'category'
    	ORDER BY t.name ASC, p.post_date DESC;"
    );
Viewing 1 replies (of 1 total)
  • The topic ‘Query Taxonomy for Custom Post Type’ is closed to new replies.