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!