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">
        <p>12 September 2014</p>
        <a href="the_permalink>"Post title 1</a>
        <p>11 August 2014</p>
        <a href="the_permalink>"Post title 2</a>
        <p>10 May 2014</p>
        <a href="the_permalink>"Post title 3</a>
        <p>20 June 2013</p>
        <a href="the_permalink>"Post title 4</a>
        <p>19 April 2013</p>
        <a href="the_permalink>"Post title 5</a>
        <p>18 January 2013</p>
        <a href="the_permalink>"Post title 6</a>

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

$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!

  • 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 '14 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. – lotech May 16 '14 at 15:56

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:


$posts = new WP_Query(
        '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>


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


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

            $last_year = $year;



<?php endif ?>
| 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 :) – lotech May 19 '14 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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