WordPress Categories by year

Problem:

There is no way to specify a time period for wp_dropdown_categories or wp_list_categories so if you are in the archive of 2010 and using one of these two functions, you'll see all categories even if they have no posts in that year, instead of only categories with posts that were used in 2010.

Solution:

I wrote this function a while back when i needed it in a clients site and had many uses for it since.

<?php
/* Create taxomony term list of posts based on given year 
*
* @param $year - (int) 4 digits - the year to return the list of. required
*
* @param $taxonomy - (string) the taxonomy name of which the retreve the terms. required
* 
* @param $echo_out - (boolean) true - to echo false to return default false.
* 
* @param $type - (array) of args that tels the function what to get.
*
* examples: 
select dropdownlist:
'type' => 'select', //(string)
'name' => 'cat', //(string) name of the select dropdown.
'js' => ture // (boolean) wheter or not to pring out onchange javascript redirect function.

list:
'type' => 'list', //(string)
'before_list' => '<ul>', //(string) html tag (<ul>,<div> you can add classes here).
'after_list' => '</ul>', //(string) html tag (</ul>,</div>)
'before_link' => '<li>', //(string) html tag (<li>,<div>,<span> you can add classes here).
'after_link' => '</li>' //(string) html tag (</li>,</div>,</span>,<br/>).

return array:
'type' => 'return-list', //(string)
'return' => 'ID' // (string) what to return (accepts ID or name).



*/
function yearly_taxonomy_list($year,$type, $taxonomy,$echo_out = false){
    global $post;
    $args = array(
        'year' => $year,
        'posts_per_page' => -1
    );
    $the_query = new WP_Query( $args );
    $cats = array();
    while ($the_query->have_posts()){
        $the_query->the_post();
        $curent_cats = wp_get_object_terms( $post->ID, $taxonomy);
        foreach ($curent_cats as $c){
            if (!in_array($c,$cats)){
                $cats[] = $c;
            }
        }
    }
    //dropdown
    if (isset($type['type']) && $type['type'] =='select'){
        $out = '<select name="'.$type['name'].'" id="'.$type['name'].'" >';
        foreach ($cats as $cd){
            $out .= '<option value='.get_term_link( $cd->term_id, $taxonomy ).'">'.$cd->name.'</option>';
        }
        $out .= '</select>';

        if($type['js']){
            $js = '<script type="text/javascript">
            var dropdown = document.getElementById("'.$name.'");
            function onCatChange() {
                if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
                    location.href = dropdown.options[dropdown.selectedIndex].value;
                }
            }
            dropdown.onchange = onCatChange;
            </script>';
            $out .=  $js;
        }
    }elseif (isset($type['type']) && $type['type'] =='list'){
        $out = $type['before_list'];
        foreach ($cats as $cd){
            $out .= $type['before_link'].'<a href="'.get_term_link( $cd->term_id, $taxonomy ).'">'.$cd->name.'</a>'.$type['after_link'];
        }
        $out .= $type['after_list'];
    }
    //return array
    elseif (isset($type['type']) && $type['type'] =='return-list'){
        //array of  IDS
        if ($type['return'] =='ID'){
            foreach ($cats as $cd){
                $out[] = $cd->term_id;
            }
        }
        //array of  names
        if ($type['return'] =='name'){
            foreach ($cats as $cd){
                $out[] = $cd->name;
            }
        }
        return $out;
    }
    else{
        $out = 'something went worng';
    }
    if($echo_out){
        echo $out;
    }else{
        return $out;
    }
}
?>

Usage:

<?php yearly_taxonomy_list($year,$type, $taxonomy,$echo_out = false); ?>

Install:

Copy this code in to your theme's functions.php or create a plugin file for it.

Parameters:

$year - (int) 4 digits - the year to return the list of. required

$taxonomy - (string) the taxonomy name of which the retreve the terms. required

$echo_out - (boolean) true - to echo false to return default false.

$type - (array) of args that tels the function what to get. see examples below.

Examples of usage:

<?php
// category dropdown with JavaScript OnChange redirect
$type=array('type' => 'select', 'name' => 'cat','js' => ture );
yearly_taxonomy_list(2010,$type, 'category' ,true);

//unordered list of categories
$type=array(
'type' => 'list', 
'before_list' => '<ul class="yearly-cats">', 
'after_list' => '</ul>', 
'before_link' => '<li>',
'after_link' => '</li>');
yearly_taxonomy_list(2010,$type, 'category' ,true);

//return array ids of categories 
$type=array('type' => 'return-list', 'return' => 'ID');
$cat_ids = yearly_taxonomy_list(2010,$type, 'category' ,true);
?>

Ohad Raz

WordPress Consultant, a WordPress Developer and a WordPress Freelancer With over 10 years experience in architecting web sites and applications. WordPress Development moderator and somethimes Plugin Developer.