Filter and Hooks

SQL Query to get the archive / year by post type

For listing the published year of the particular post type, we can use the SQL as presented below.

<ul id="termsYear">
<?php $years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = 'media' AND post_status = 'publish' GROUP BY year DESC" );
foreach ( $years as $year ) {?>
	<li><a data-val='<?=$year->year?>' class="year"><?=$year->year?></a></li>
<?php } ?>

Replace the ‘post_type’ with your own post type.

Views: 22