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
- Why use if function_exists?
- Use AJAX in shortcode
- Include files in child theme functions file
- Filter the query ONLY for the search results page
- How to add attributes to a shortcode
- Delete expired posts after a number of days after they expired
- How to use get_template_directory_uri() to load an image that is in a sub-folder of my theme?
- How to override functions.php in child theme?
- Is it possible to maintain image aspect ratio when scaling images?
- How to override WordPress registration and insert an auto-generated username?
- How often is functions.php loaded?
- update_post_meta for custom field not working upon form submission
- how to get and display logged in user’s recently read posts
- Cleaner way to access custom fields in code?
- Add a new subscriber role using a function
- Why functions.php file is called 8 time for just one page load?
- How to set the jpg image compression for specific thumbnail sizes?
- Function to auto-set a featured image that is already in use
- Delete pages and Create default pages for all new network sites
- Post thumbnail relative link and HTML modify
- Find first image on paginated post for Pinterest
- How would go about if I just want a temporary function?
- How do I edit wp_head and/or functions.php to remove rss-feed which isnt used and dont validate?
- Generating rel=prev and rel=next only on wordpress categories
- How do I get a child theme to load scripts from the parent theme?
- Access / Filter block variations in Gutenberg, in WordPress 5.6
- example of build_query function?
- Avoid loading css from parent theme
- How to hide unused profile fields?
- async javascript and css for wordpress
- Override widget in function WordPress
- WooCommerce – Want to show multiple currency for a single product [closed]
- How add various functions within 1 conditional?
- Disable auto-resizing of uploaded images, but only for certain filename
- Add to cart php not working [closed]
- Create cron job for update translations automatically
- Login Redirect if Logged in from Specific Page
- Automatically include all php files in a child theme directory
- Override the function twentytwelve_scripts_styles in a child theme
- Custom taxonomy widget in admin area
- Removing specific menu items?
- Do I need to use wp_reset_postdata for my function?
- Use a shortcode to display custom meta box contents
- how to make has_block() see inside blocks too
- add_theme_support( ‘title_tag’ ) is not showing title on index.php
- Tracking the number of shortcodes for a list?
- Solution dealing with Child Theme / Parent theme functions
- remove edit link only for published post and pending post
- When are the user meta fields created in the database? Admin vs Woo API REST
- is_product_category(‘Services’) not working
- How to automatically add counted number to navigation menu items?
- Replace theme function
- How to access custom class methods from any include without using global
- Insert Modal on user first login
- What is best practices to move the following code into a function?
- Correct way of Enqueue self hosted fonts in sass project
- function syntax is off,
- Change category checkbox into radio button [duplicate]
- wp_enqueue_script only works outside of action
- How can I include user meta information in the resulting array of a WP_User_Query?
- How to add next height number in Order Attributes inside the Add new page. [duplicate]
- Show the online status of the current post’s author
- Get Value of Custom Field
- How does child theme functions.php work with parent theme functions.php? Is it like CSS? [duplicate]
- restrict access to specific urls on a specific period of time
- Need a functions that adds (adm)/(mod) if current user is admin/moderator right after their username
- Add title & subtitle to shortcodes
- How to Delete Posts by title?
- clickable title of 2 post on mainpage USING functions.php
- Display function from functions.php in tag.php
- AJAX values converted to PHP Variables?
- Custom Post Type Search