Hi - I am struggling to find a way to build an archive list of “posts” in a CMS.
(Backstory, briefly: I’m using WordPress and the new “custom post types” but these do not work with WP’s built-in archive-building template tags).
I need to go into my DB, grab all the “years” from each post’s published date, and from that build a simple list of years in which posts where published. For example, if my DB has posts published in 2008, 2009, and 2010, all I need is those three years (not how many posts or anything else). Then I can build a list and make each year link to an archive page specific to that year.
Logically, in terms of steps, I know what I want to do:
[INDENT]1. Query the MySQL DB for all posts and retrieve content from one field - e.g. post_date
- this field is stored by default in the format of ‘YYYY-MM-DD 00:00;00’
-
I only need the year (‘YYYY’) so I need to pull that out - the rest of the
post_date
data is not needed… -
Sort and group those year values such that I get a list of years in which posts where published.[/INDENT]
I know the MySQL query.
The problem is actual language and syntax - I don’t know what PHP functions to use. Specifically:
[INDENT]1. I can assume there will be a loop of some sort, with an increment counter to go through all the returned values and search for the years (‘YYYY’) - a ‘for’ loop?
-
How would I identity and strip out the YYYY from each string?
-
How would I sort and group those YYYY’s into a list? I’m guessing the end result here should be an array. And again, I don’t need to know that I have, for example, 28 posts in 2009. That is, I don’t need “2009” listed 28 times. I only need to know that 2009 is a valid year value.
-
Once I have that list of years, how would I separate them out to be individual strings to then work with - e.g. if I have [2008, 2009, 2010], I need to be able to grab each one individually and do stuff with it (wrap it in some html). Again, I assume this would be a ‘for’ or ‘while’ loop?[/INDENT]
I’m sure this is pretty basic stuff. I’m just stuck on the specific syntax / functions to use. Any suggestions, tips would be great!