3

What have made me pulling my hair apart for while, how to output category specific month list. I've been searching for the same problem and solution cannot find any lucky so far...

Anyway, I have two POST type pages in my web site. One is NEWS page(ex. http://example.com/news/) and the other is BLOG page(ex. http://example.com/blog/).

I use archive.php to show my NEWS and BLOG page and where I want to output month list with post counts in sidebar.php. I could output lists by using wp_get_archives('cat=0') however this outputs like this

2013/05(20)
2013/06(12)
2013/07(18)
2013/08(6)

HTML:

<li><a href="http://xxxxxxxx.com/wp/2013/05/"></a></li>
<li><a href="http://xxxxxxxx.com/wp/2013/06/"></a></li>
<li><a href="http://xxxxxxxx.com/wp/2013/07/"></a></li>
<li><a href="http://xxxxxxxx.com/wp/2013/08/"></a></li>

When I click one of these, this takes me to the page where all the posts happened in that specific month, of course.

What I'd like to output if I am in NEWS page for example is:

HTML:

<li><a href="http://xxxxxxxx.com/wp/news/2013/05/"></a></li>
<li><a href="http://xxxxxxxx.com/wp/news/2013/06/"></a></li>
<li><a href="http://xxxxxxxx.com/wp/news/2013/07/"></a></li>
<li><a href="http://xxxxxxxx.com/wp/news/2013/08/"></a></li>

Category specific. Any idea?

1 Answer 1

1

you can use getarchives_where filter to get archives by category

add_filter( 'getarchives_where', 'my_archives_filter_function', 10, 2 );

// your filter function replace YOUR CATEGORY ID with category term id e.g(3)
function my_archives_filter_function($where) {
   global $wpdb;
   $where ." AND $wpdb->posts.ID IN (SELECT $wpdb->posts.ID 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) WHERE $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id = 'YOUR CATEGORY ID')";
   return $where;
}

now call wp_get_archives();

3
  • thanks @Anjum I am bit new to WP. So what I gotta do is just copy & paste your code into my sidebar.php, right? It doesn't seem working so far...
    – norixxx
    Commented Aug 27, 2013 at 2:53
  • Try functions.php
    – jimihenrik
    Commented Mar 27, 2015 at 23:05
  • Won't this affect all archive pages? I thought the question asked was how to create a date archive page for each category on the site... Commented Oct 15, 2015 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.