Your question is pretty old, but I just wanted to add a real solution to your question. Here’s a function that will return an array of years you have posts published in. You can put this function in functions.php or in a plugin or whatever you want.
function get_posts_years_array() {
global $wpdb;
$result = array();
$years = $wpdb->get_results(
$wpdb->prepare(
"SELECT YEAR(post_date) FROM {$wpdb->posts} WHERE post_status="publish" GROUP BY YEAR(post_date) DESC"
),
ARRAY_N
);
if ( is_array( $years ) && count( $years ) > 0 ) {
foreach ( $years as $year ) {
$result[] = $year[0];
}
}
return $result;
}
You’ll need to modify it slightly for custom post types… just add AND wp_posts.post_type="my_cpt"
somewhere in there.
Related Posts:
- Remove “Category:”, “Tag:”, “Author:” from the_archive_title
- Order posts (across the whole site) by metadata date
- Date/time limitation of posts where function must be executed
- Individually styling date month year [closed]
- How to set default archive image without overriding first attached image? [closed]
- Ordering posts by publish date not working?
- How do display most popular post from a year earlier to the day?
- Updating Media Published Date When Parent Post Is Modified in WordPress
- Pagination in relation to archive.php
- Function filter breaking tag archive menus
- Remove the month and year from a WordPress Date?
- How to display the_archive_title() and the_archive_description() – “weird” interaction
- Image loading function not working on archive.php template
- How do I display a date correctly that is stored in the database as a backwards 8 digit number from Advanced Custom Fields? [closed]
- Getting images from media library and get_the_date() not working
- WordPress show bad the php hour and date
- Overwriting TwentyTwelve template file with child theme template, but lower in the hierarchy
- Changing date query from month
- Simplest Way to Build Custom Archives Page?
- wordpress built-in Jalali date convertor
- Create a CUstom Archive by Year, but just for a Single Category
- Exclude specific post from query
- ACF Date Form in Custom Admin Field
- Changing author links on homepage to buddypress profiles – without affecting ability to link to author archive throughout the site
- Featured image on archive page based on post type
- Replacing term/taxonomy archive pages with search queries
- Generate daily archive whenever any post type is added
- Change Post status based on custom field date +1 day
- Missing feature image link function
- What’s the difference between home_url() and site_url()
- get_template_directory_uri pointing to parent theme not child theme
- How to customize the_archive_title()?
- remove empty paragraphs from the_content?
- What is the “with_front” rewrite key?
- Why use if function_exists?
- How to override parent functions in child themes?
- wp_enqueue_script was called incorrectly
- Add multiple custom fields to the general settings page
- Ajax call always returns 0
- 400 bad request on admin-ajax.php only using wp_enqueue_scripts action hook
- How long does a deprecated function live in core?
- Solution to render Shortcodes in Admin Editor
- How to add a data attribute to a WordPress menu item
- What’s the difference between esc_html, esc_attr, esc_html_e, and so on?
- remove_action on after_setup_theme not working from child theme
- plugins_url vs plugin_dir_url
- Remove type attribute from script and style tags added by WordPress
- How to run a function every 5 minutes?
- Custom Post Type Archives by Year & Month?
- Best way of passing PHP variable between partials?
- Upload Multiple Files With media_handle_upload
- How to display custom field in woocommerce orders in admin panel?
- Adding fields to the “Add New User” screen in the dashboard
- Issues with title-tag and document_title_parts
- How do I get the current edit page ID in the admin?
- How to check if a user exists by a given id
- Why isn’t is_page working when I put it in the functions.php file?
- Add tags to the section via functions.php
- Add image size if page template
- How to create a custom order status in woocommerce!
- Remove Actions/Filters added via Anonymous Functions
- Adding a second email address to a completed order in WooCommerce [closed]
- How to load parent_theme functions.php before child_theme?
- How to load scripts/styles specific for a page
- Programatically add options to “add new” custom field dropdown
- Is there any global functions.php file which works for any theme?
- Excluding iPad from wp_is_mobile
- When should you, and when should you not, use wp_list_pluck()?
- Get the ID of the page a menu item links to?
- Add container to nav_menu sub menu
- Difference between the_permalink() and get_permalink() function
- What’s the difference between WordPress random_int() and PHP built-in function random_int()?
- Child theme – Overriding ‘require_once’ in functions.php
- Changing the post date and time with function
- Link to user’s profile settings page?
- WordPress Enqueue for homepage only, functions.php, wp-framework
- get php variable from functions php and echo it in theme template files [closed]
- Get menu object from theme_location
- Is it ok to use a function to output the text domain name in a wordpress theme
- Displaying the number of updates available in the Admin area
- Trying to use add_action and do_action with parameters
- Get rid of WordPress category, tag and author archives?
- Use AJAX in shortcode
- Set JPEG compression for specific custom image sizes
- Can the new 4.8v text widget visual editor be removed?
- Prevent “main” WPMU site_url() being returned in functions
- Include files in child theme functions file
- Memorizing syntax
- Custom page with variables in url. Nice url with add_rewrite_rule
- Define page template in wp_insert_post
- Which WP functions do you need to use esc_html() or esc_url() on?
- Check if post is being published for the first time, or is an already published post being updated
- Display random categories on the front page (Finding and Editing Theme Functions)
- How to redirect to post if search results only returns one post
- remove links from images using functions.php
- Use author author display name in permalink structure for pages and posts
- Is it possible to rename a post format?
- Why does WordPress have private functions?
- Import WordPress XML File from Within Functions.php
- What is this code in my theme’s functions.php? if (isset($_REQUEST[‘action’]) && isset($_REQUEST[‘password’])