title: Custom post type + taxonomy term yearly/monthly archive
body: <p>-</p>
  
  <p>I have been researching on how to create a date archive for a custom post which has a taxonomy and various terms...</p>
  
  <p>I have had great success with this <a href="https://stackoverflow.com/questions/14486792/custom-post-type-yearly-monthly-archive/14841924#14841924?newreg=24ba100fea444880b3e2f746a9776b89">Stackoverflow answer</a> from walltea however I need some help with modifying it to also take into account taxonomies and its terms...</p>
  
  <p>Is this possible?</p>
  
  <p><em>--EDIT--</em></p>
  
  <p><strong>Ok I managed to do this after LOADS of time on it, If you know if a better solution to this then please do share as the way I found is a little bit cumbersome:</strong> (again original code curtosy of walltea's <a href="https://stackoverflow.com/questions/14486792/custom-post-type-yearly-monthly-archive/14841924#14841924?newreg=24ba100fea444880b3e2f746a9776b89">Stackoverflow answer</a>)</p>
  
  <p><strong>1.</strong>
  In your functions.php place this code: (changing the custom posts types that you have for '$cptPart')</p>
  
  <pre><code>/**
  * Custom post type date archives
  */
  
  /**
  * Custom post type specific rewrite rules
  * @return wp_rewrite             Rewrite rules handled by Wordpress
  */
  
  add_action('generate_rewrite_rules', 'cpt_rewrite_rules');
  
  /**
  * Generate date archive rewrite rules for a given custom post type
  * @param  string $cpt        slug of the custom post type
  * @return rules              returns a set of rewrite rules for Wordpress to handle
  */
  function cpt_rewrite_rules($wp_rewrite) {
  $rules = array();
  
  $cptPart = "(news_events|live_reports|book_review|readers_digest|publications|jobs)";
  $taxonomyPart = "([a-zA-Z_-]+)";
  $termPart = "([a-zA-Z_-]+)";
  $patterns = array(
  array(
  'rule' =&gt; "$cptPart/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/$taxonomyPart/$termPart",
  'vars' =&gt; array('post_type', 'year', 'monthnum', 'day', 'taxonomy', 'term')
  ),
  array(
  'rule' =&gt; "$cptPart/([0-9]{4})/([0-9]{1,2})/$taxonomyPart/$termPart",
  'vars' =&gt; array('post_type', 'year', 'monthnum', 'taxonomy', 'term')
  ),
  array(
  'rule' =&gt; "$cptPart/([0-9]{4})/$taxonomyPart/$termPart",
  'vars' =&gt; array('post_type', 'year', 'taxonomy', 'term')
  ),
  array(
  'rule' =&gt; "$cptPart/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/$taxonomyPart",
  'vars' =&gt; array('post_type', 'year', 'monthnum', 'day', 'taxonomy')
  ),
  array(
  'rule' =&gt; "$cptPart/([0-9]{4})/([0-9]{1,2})/$taxonomyPart",
  'vars' =&gt; array('post_type', 'year', 'monthnum', 'taxonomy')
  ),
  array(
  'rule' =&gt; "$cptPart/([0-9]{4})/$taxonomyPart",
  'vars' =&gt; array('post_type', 'year', 'taxonomy')
  ),
  array(
  'rule' =&gt; "$cptPart/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})",
  'vars' =&gt; array('post_type', 'year', 'monthnum', 'day')
  ),
  array(
  'rule' =&gt; "$cptPart/([0-9]{4})/([0-9]{1,2})",
  'vars' =&gt; array('post_type', 'year', 'monthnum')
  ),
  array(
  'rule' =&gt; "$cptPart/([0-9]{4})",
  'vars' =&gt; array('post_type', 'year')
  ),
  );
  
  foreach ($patterns as $pattern) {
  $i = 1;
  $params = array();
  foreach ($pattern['vars'] as $var) {
  $params[] = $var . '=' . $wp_rewrite-&gt;preg_index($i);
  $i++;
  }
  $query = 'index.php?' . implode('&amp;', $params);
  
  $rules[$pattern['rule']."/?$"] = $query;
  $rules[$pattern['rule']."/feed/(feed|rdf|rss|rss2|atom)/?$"] = $query."&amp;feed=".$wp_rewrite-&gt;preg_index($i);
  $rules[$pattern['rule']."/(feed|rdf|rss|rss2|atom)/?$"] = $query."&amp;feed=".$wp_rewrite-&gt;preg_index($i);
  $rules[$pattern['rule']."/page/([0-9]{1,})/?$"] = $query."&amp;paged=".$wp_rewrite-&gt;preg_index($i);
  }
  
  $wp_rewrite-&gt;rules = $rules + $wp_rewrite-&gt;rules;
  return $wp_rewrite;
  }
  
  /**
  * Get a montlhy archive list for a custom post type
  * @param  string  $cpt  Slug of the custom post type
  * @param  boolean $echo Whether to echo the output
  * @return array         Return the output as an array to be parsed on the template level
  */
  function get_cpt_archives($cpt, $echo = false, $taxomony = null, $term = null)
  {
  global $wpdb;
  
  $conditions = "";
  if ($taxomony) {
  $conditions .= " AND wtt.taxonomy LIKE %s";
  if ($term) {
  $conditions .= " AND wt.slug LIKE %s";
  }
  }
  $sql = $wpdb-&gt;prepare("
  SELECT * 
  FROM wp_posts p
  LEFT JOIN wp_term_relationships wtr ON wtr.object_id = p.ID
  LEFT JOIN wp_term_taxonomy wtt ON wtr.term_taxonomy_id = wtt.term_taxonomy_id
  LEFT JOIN wp_terms wt ON wtt.term_id = wt.term_id
  WHERE post_type = %s
  AND post_status = 'publish'
  $conditions
  GROUP BY YEAR(p.post_date),
  MONTH(p.post_date)
  ORDER BY p.post_date DESC
  ", $cpt, $taxomony, $term);
  
  $results = $wpdb-&gt;get_results($sql);
  
  if ( $results )
  {
  $archive = array();
  foreach ($results as $r)
  {
  $year = date('Y', strtotime( $r-&gt;post_date ) );
  $month = date('F', strtotime( $r-&gt;post_date ) );
  $month_num = date('m', strtotime( $r-&gt;post_date ) );
  $link = get_bloginfo('siteurl') . '/' . $cpt . '/' . $year . '/' . $month_num;
  if ($taxomony) {
  $link .= "/" . $r-&gt;taxonomy;
  if ($term) {
  $link .= "/" . $r-&gt;slug;
  }
  }
  $this_archive = array( 'month' =&gt; $month, 'year' =&gt; $year, 'link' =&gt; $link );
  array_push( $archive, $this_archive );
  }
  
  if( !$echo )
  return $archive;
  foreach( $archive as $a )
  {
  echo '&lt;li&gt;&lt;a href="' . $a['link'] . '"&gt;' . $a['month'] . ' ' . $a['year'] . '&lt;/a&gt;&lt;/li&gt;';
  }
  }
  return false;
  }
  </code></pre>
  
  <p>-</p>
  
  <p><strong>2.</strong> In your custom archive file (for example one of mine was archive-publications.php) place this code above the loop:</p>
  
  <pre><code>    &lt;?php 
  function lr_posts_join($join) {
  global $taxonomy_global_join;
  global $term_global_join;
  if ($taxonomy_global_join) $join .= " $taxonomy_global_join";
  if ($term_global_join) $join .= " $term_global_join";
  return $join;
  }
  function lr_posts_where($where) {
  global $taxonomy_global_where;
  global $term_global_where;
  if ($taxonomy_global_where) $where .= " $taxonomy_global_where";
  if ($term_global_where) $where .= " $term_global_where";
  return $where;
  }
  add_filter('posts_join', 'lr_posts_join');
  add_filter('posts_where', 'lr_posts_where');
  
  $extraArgs = "";
  if (get_query_var('taxonomy')) {
  $taxonomy_global_join = "
  LEFT JOIN $wpdb-&gt;term_relationships wtr ON wtr.object_id = $wpdb-&gt;posts.ID
  LEFT JOIN $wpdb-&gt;term_taxonomy wtt ON wtr.term_taxonomy_id = wtt.term_taxonomy_id ";
  $taxonomy_global_where = " AND wtt.taxonomy LIKE '" . esc_sql(like_escape(get_query_var('taxonomy'))) . "'";
  $extraArgs .= "&amp;taxonomy=" . get_query_var('taxonomy');
  
  if (get_query_var('term')) {
  $term_global_join = "
  LEFT JOIN $wpdb-&gt;terms wt ON wtt.term_id = wt.term_id ";
  $term_global_where = " AND wt.slug LIKE '" . esc_sql(like_escape(get_query_var('term'))) . "'";
  $extraArgs .= "&amp;term=" . get_query_var('term');
  }
  }
  
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $posts = query_posts("post_type=publications&amp;paged=$paged$extraArgs");
  $taxonomy_global_join = $taxonomy_global_where = '';
  $term_global_join = $term_global_where = '';
  
  while (have_posts()) : the_post(); ?&gt;
  
      -- loop code here --
  
  &lt;?php endwhile; ?&gt;
  </code></pre>
  
  <p><strong>3.</strong> The above code lets our archive page understand our filtering for taxonomies and terms. Save your files and re-save your permalinks in the admin area. Next lets use this puppy! I used this in my custom sidebar (sidebar-publications.php):</p>
  
  <pre><code>&lt;ul&gt;
      &lt;?php echo get_cpt_archives( 'publications', true, 'publication_category', 'articles' ) ?&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>Our function gets the custom post (Publications) and 'true' is set so we want the normal list layout. Next it calls the taxonomy we want... I just have one for this custom post ( publication_category ) and finally the term I want in that taxonomy ( articles ). This should spit out a link like this: www.website/custom_post/year/month/taxonomy/term</p>
  
  <p>I hope this code serves you well! My client is very happy ;)</p>
  
  <p>Very Best,
  Chris</p>