Support » Fixing WordPress » Custom Post Types and Sorting by Category or Year

  • I have a couple questions on how to do certain things with a custom post type and I wasn’t able to find anyone solution that worked perfectly for me on the support forums here or on google.

    The first question would be how would I do the following. I have a custom post type called “discography”, and I want to sort the output by Albums, EPs and Singles which are Categories.

    Every example I have come across either outputs nothing, or outputs everything (All Albums, EPs and Singles together). How can I group these separately? Or only retrieve say just “EPs”.

    My second question is what is the best way to group by year posted of Custom post types?

    The following code I got to work:

    <?php if (have_posts( 'atc-cover-songs' )) : while (have_posts( 'atc-cover-songs' )) : the_post( 'atc-cover-songs' ); ?><?php
    	$covers = new WP_Query('post_type=atc-cover-songs&posts_per_page=-1');
    while ( $covers->have_posts() ) : $covers->the_post();
    $year = the_date('Y', '', '', FALSE);
    
    // If your year hasn't been echoed earlier in the loop, echo it now
    if ($year !== $year_check) {
    	if (empty($year)) {
    
    	} else {
    		if (empty($year_check) and ($duplicate_year == true)){
    
    		}else {
    			//echo '<h2>' . $year . 'open' . $year_check . '</h2>';
    			if ($year_check > $year and ($year_check == 2015)) {
    				if ($duplicate_closing == false) {
    
    ?>
    				</ul>
                </div>
                <?php
    				}
    			}
    			if ($year == 2013 and ($year_check == 2014)) {
    
    ?>
    				</ul>
    			</div>
    			<?php
    				$closeduplicate = true;
    			}
    
    			if ($year == 2012 and ($year_check == 2013)){
    				?>
    					</ul>
    				</div>
    			<?php
    			}
    
    ?>
    	<div class="related-article">
                    <h2 class="box-title">Covers from <?php echo $year ?></h2>
                    <ul class="row">
    
                <?php
    		}
    	}
    }
    
    $post_thumbnail = get_the_post_thumbnail( get_the_ID(), 'thumbnail' );
    
    $class_format = '';
    if  ( ! $post_thumbnail )
    	$class_format = 'fa-format-' . get_post_format( get_the_ID() );
    
    printf(
    	'<li class="col-md-6">
                                                <a href="%s" class="post-thumbnail %s">%s</a>
                                                <div class="related-post-content">
                                                    <a class="related-post-title" href="%s">
                                                    <span class="date">%s</span></br>
                                                    %s</a>
                                                </div>
                                            </li>',
    	esc_url( get_permalink() ),
    	$class_format,
    	$post_thumbnail,
    	esc_url( get_permalink() ),
    	get_the_date(),
    	get_the_title()
    );
    ?>
    	<?php
    if (empty($year_check)) {
    	//Do nothing
    }elseif ( $year !== $year_check)  {
    	if (empty($year) and ($duplicate_year == true)){
    		if ($year == 2014 and ($year_check == 2015)) {
    			$duplicate_closing = true;
    		}
    	}else{
    		//echo '<h2>' . $year . 'close' . $year_check . '</h2>';
    		if ($year_check > $year and ($year_check == 2015)){
    		}else{
    			if ($closeduplicate == true) {
    			} else {
    ?>
    				</ul>
                </div>
    
                               <?php
    			}
    		}
    	}
    }
    // Now that your year has been printed, assign it to the $year_check variable
    $year_check = $year;
    if ($year_check == 2015) {
    	$duplicate_year = true;
    }
    ?>
    
    <?php endwhile; ?>

    However, as you can see I have a lot of check variables and if statements. Why, because without them this code doesn’t work. I end up with 4 posts grouped as 2015, then 3 more posts grouped as 2015. They don’t fall into one group of 2015.

    While the if statements work with the posts I have now, I find my self having to adjust them each time I add a new posts or every couple new posts because things break again.

    So with that being said, does anyone have any advice, suggestions or code examples that will get the job done that I’m looking for? Thank you so much.

  • The topic ‘Custom Post Types and Sorting by Category or Year’ is closed to new replies.