On a recent modification to an existing site I was asked to create a gallery section where the site admin could upload images

As usual, I turned to the ever so adaptable plugin Advanced Custom Fields (ACF) with it’s very versatile gallery field, along with a custom post type (CPT) to hold the photos in. On the front end I had isotope filters working on my archives-photos.php file which was also showing all posts from our CPT.

I had a problem though with creating a list of years for our filters as the function wp_get_archives is very limited in the amount of control you have on it’s HTML output and I needed to create a list similar to the one below with a data-filter attribute in order for the isotope filters to actually work in sorting the list of galleries.

The solution came from a wpbeginner.com article which had a snippet of code to run a custom query against the WordPress database to extract all the years for our CPT. I modified their code slightly as their’s was outputting months as well, grouping them into years, where as al I wanted was the year. The final code is below. I hope it helps someone else trying to modify the output of wp_get_archives.