1
votes

I have a bit of a strange problem with my WP-query. I have a custom post type (portfolio), with a custom taxonomy called year. I have categories for each year, so what I want to do is display all posts for each year. The problem is, only 2012 works. Doesn't matter if I order the categories ASC/DESC - only 2012 works.

<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
        $posts = get_posts('taxonomy=year&post_type=portfolio&year=' . $category->slug);
        foreach($posts as $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->slug . ']" >';
            the_post_thumbnail('thumbnail');
            echo '</a>';
            ?>
            </div>
        <?php endforeach; ?>                    
        </div>  
    </article>
    <?php 
    endforeach;
    wp_reset_query();
    ?>
</section>

What am I doing wrong? To me, it seems right.. I've tried a bunch of different takes on this, everything from real querys to ridiculous sortings but I just can't get it right..

Thank you in advance!

1
get category list using custom post type name: wp.me/p4esuX-3kRavi Patel

1 Answers

0
votes

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).