Sunday, January 13, 2013

How to list archive years per category in wordpress

This post will teach you how to list down archive years per category in your wordpress site. The script below is using SQL statement that will query from your wordpress table and the tables involve are: posts, term_relationships, term_taxonomy, and terms.

"SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.taxonomy='category') INNER JOIN $wpdb->terms ON ($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id) WHERE $wpdb->terms.slug='<your category slug here>' ORDER BY post_date DESC"

To get the archive years per category slug, you just need to supply it below.

.... WHERE $wpdb->terms.slug='<your category slug here>' ORDER BY post_date DESC"); 

If you want to select using your category name, then change the condition above with name field $wpdb->terms.name.

.... WHERE $wpdb->terms.name='<your category name here>' ORDER BY post_date DESC"); 

<ul>
<?php

$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.taxonomy='category') INNER JOIN $wpdb->terms ON ($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id) WHERE $wpdb->terms.slug='<your category slug here>' ORDER BY post_date DESC");               

foreach($years as $year) : ?>
        <li><a href="<?php echo get_year_link($year); ?>"><?php echo $year;?></a></li>                          
<?php endforeach; ?>

</ul> 

Please leave a comment if you like this post. Happy coding!!

No comments:

Post a Comment