Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've almost got what I want working from cobbling bits but I need a bit of help finishing it up. What I'm trying to do is output the following (for a custom post type) within a page template (not a separate archive page):

<ul class="accordion">
<li>
    <h3>2014</h3>
    <div>
        <p>12 September 2014</p>
        <a href="the_permalink>"Post title 1</a>
    </div>
    <div>
        <p>11 August 2014</p>
        <a href="the_permalink>"Post title 2</a>
    </div>
    <div>
        <p>10 May 2014</p>
        <a href="the_permalink>"Post title 3</a>
    </div>
</li>
<li>
    <h3>2013</h3>
    <div>
        <p>20 June 2013</p>
        <a href="the_permalink>"Post title 4</a>
    </div>
    <div>
        <p>19 April 2013</p>
        <a href="the_permalink>"Post title 5</a>
    </div>
    <div>
        <p>18 January 2013</p>
        <a href="the_permalink>"Post title 6</a>
    </div>
</ul>

The code I've got so far looks like this:

<?php
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = 'press-releases' AND post_status = 'publish' GROUP BY year DESC" );

foreach ( $years as $year ) {
    $posts_this_year = $wpdb->get_results( "SELECT post_title FROM wp_posts WHERE post_type = 'press-releases' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );

    echo '<ul class="accordion">';
    echo '<li><h3>' . $year->year . '</h3>';
    foreach ( $posts_this_year as $post ) {
        echo '<div>' . $post->post_title . '</div>';
    }
    echo '</li>';
    echo '</ul>';
}
?>

Which is the basics of what I want. However, I also need to wrap the post title in the permalink and output the the date the post was published. I'm sure it's a simple fix but I don't know how to do it. Any ideas or is there a better way to do what I'm trying to achieve?

Any input appreciated, thanks!

share|improve this question
1  
This is very far from typical iterating posts in WordPress via Loop mechanics. Why had you started on this from raw SQL? –  Rarst May 16 at 15:32
    
To be perfectly honest, I searched low and high and this was the only code I could find that replicated what I needed in terms of output. If I could achieve this within a neat loop that would be excellent but I'm not sure where to start. It seems most of the built in WordPress functions are geared towards outputting archives purely on their own page. I'm struggling to find a solution that allows me to pull this information through into a page template. –  user1001657 May 16 at 15:56

1 Answer 1

up vote 1 down vote accepted

You can just use a normal query to grab all posts (which are, by default, sorted date descending), and break them up as you loop over them:

<?php

$posts = new WP_Query(
    array(
        'posts_per_page' => -1,
        'post_type' => 'press-releases'
    )
);

if ( $posts->have_posts() ) : ?>

    <ul class="accordion"><?php

        while ( $posts->have_posts() ) : $posts->the_post();
            $year = get_the_time( 'Y' );

            if ( $posts->current_post === 0 )
                printf( '<li><h3>%s</h3>', $year ); // First post, always open the <li>
            elseif ( $last_year !== $year )
                printf( '</li><li><h3>%s</h3>', $year ); // Unlike above, close the previous open <li>

        ?>

            <div>
                <p><?php the_time( 'j F Y' ) ?></p>
                <a href="<?php the_permalink() ?>"><?php the_title() ?></a>
            </div>

        <?php           

            if ( ( $posts->current_post + 1 ) === $posts->post_count )
                echo '</li>'; // Always close the <li> at the end of the loop

            $last_year = $year;

        endwhile;

    ?></ul>

<?php endif ?>
share|improve this answer
1  
You, sir, are an absolute god send! This may seem trivial to you but I learnt a lot from that post – a big thanks for taking the time to help with this. I'm totally comfortable with the all the wp_query parts but sorting through it is my trouble. Your solution seems to output exactly what I needed so I'll mark it as such :) –  user1001657 May 19 at 10:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.