• Hello! Im incredibly confused to start this thread of, mainly with the terms of everything. So please bare with me.

    I have a custom post type (portfolio), with a taxonomy (year) which has catogories (2012, 2011.. – are they called terms?). I want to print All posts from ‘year’, sorted DESC by the categories. Like this:

    2012
    – post 1
    – post 2

    2011
    – post 1
    – post 2

    etc etc.

    I’ve googled for (seriously) hours now, but since I don’t know the correct terms of the elements it’s just confusing me even more.. Thanks in advance anyways!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter mYrAn

    (@myran)

    Im now getting the categories, but no posts..

    <?php get_header(); ?>
    	<section id="content">
    	<?php
    	$categories = get_categories('taxonomy=year&order=DESC');
    
    	foreach($categories as $category) : ?>
    
    	<article class="year">
    		<h2><?php echo $category->name ?></h2>
    		<div class="items">
    		<?php
    		$args = array(
    			'category_name' => $category->slug,
    			'posts_per_page' => -1
    		);
    		query_posts($args) ?>
    		<?php
    		while(have_posts()) : the_post(); ?>
    
    		<div class="item">
    		<?php
    		$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
    		echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" rel="lightbox[' . $category->name . ']" >';
    		the_post_thumbnail('thumbnail');
    		echo '</a>';
    		?>
    		</div>
    		<?php endwhile; ?>
    		</div>
    	</article>
    	<?php
    	endforeach;
    	wp_reset_query();
    	?>
    </section>
    <?php get_footer(); ?>
    Thread Starter mYrAn

    (@myran)

    Im just getting more and more confused by this.. Now I can get posts, from 2012 but not 2011 (????).

    $posts = get_posts('taxonomy=year&post_type=portfolio&year=' . $category->slug);

    Thread Starter mYrAn

    (@myran)

    I’ve solved it myself now, still not getting it 100% but it works at least.. There has got to be some smarter way of doing this, since im now looping trough all images for every term. Well, here’s the code (get posts grouped by term from custom taxonomy).

    <section id="content">
    	<?php
    	$categories = get_categories('taxonomy=year&order=DESC');
    
    	foreach($categories as $category) { ?>
    
    		<article class="year">
    			<h2><?php echo $category->name ?></h2>
    			<div class="items">
    			<?php
    			$args = array(
    				'post_type' => 'portfolio'
    			);
    
    			query_posts($args);
    			$count = 0;
    
    			while(have_posts()) : the_post();
    				$terms = get_the_terms( $post->ID, 'year' );
    
    				foreach ( $terms as $term ) {
    					$imgslug = $term->name;
    				}
    
    		 		if($imgslug == $category->name) {
    					if($count == 6) {
    						echo '<div class="expanded-items">';
    					}
    			?>
    					<div class="item">
    					<?php
    					$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
    					echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" rel="lightbox[' . $category->slug . ']" >';
    					the_post_thumbnail('thumbnail');
    					echo '</a>';
    					?>
    					</div>
    
    					<?php
    				}
    				$count++;
    
    			endwhile;
    			if($count >= 6) {
    				echo '</div>';
    			}
    			?>
    			</div>
    			<div class="expand">Visa fler</div>
    		</article>
    	<?php } ?>
    	</section>

    That is with an expandable list, so it shows 6 from the start and then expands to show the rest of the items (jQuery).

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Get custom post type posts by taxonomy category’ is closed to new replies.