In a recent Genesis customization work I took up, the requirement was to display entries of a Custom Post Type grouped by years the posts were published in.
The output needs to be the year the latest entry was published in, then an unordered list of titles linking to their permalinks for all entries published in that year and repeat this for any/all other years.
This can be done using a single query with the help of PHP array functions and nested foreach loops.
1) We can write a custom WordPress query that loops through all the entries of the CPT and
a) retrieve the year the current post was written
b) create an empty associative array with $year as the key
c) create an indexed array from the above array with date, title and permalink keys
2) grab the actual years which are the keys of the above array
3) Loop through the above years array to
a) display the year
b) for the above year, loop through all the parent years array's key => value pairs and show linked titles
This method works on entries of any post type and any other criterion that WP_Query can have.
PHP code for the above in a site running Genesis (code inside the function can be used in any WordPress theme):
To view the full content, please sign up for the membership.
Already a member? Log in below or here.