I'll try to explain the issue and hope somebody can help me figure out the problem. It's a long post - you've been warned ;)
I'm trying to build an Archive list that will show a list of years in which posts were published in specific custom post type (e.g. post_type = "news").
And each year in that list needs to link to an archive page that (obviously) displays all published posts in that post_type for that year.
Now I know that certain built-in WordPress functions don't work natively with Custom Post Types (CPTs) - such as wp_get_archives().
There is a bit of code that supposedly adds archive functionality to wp_get_archives() for CPTs: http://bajada.net/2010/07/15/adding-custom-post-types-to-wp_get_archives
However, using that I get errors when I put wp_get_archives('type=yearly') in my templates. So alternatively I tried querying the DB directly to build the archive list, and I get the same (I think) error.
Let's say I have posts published in 2010 and 2009 in post_type = "news". I put the following in a sidebar include to build a by-year archive list for those posts:
<?php wp_get_archives('type=yearly'); ?>
That build a list of years that are links - like this:
* 2010 with a URL of http://example.com/2010
* 2009 with a URL of http://example.com/2009
Then I have a custom page template, called 'archive.php' - I am using this ONLY for the CPT = "news", no other post types on my site need/have archives. I understand the template heirarcy will go to this template when WP is looking for an archive. And that does happen, because I can see that it is indeed using my custom page template.
So on that archive.php template I have the following to query only my "news" posts:
// custom query
$args = array(
'post_type' => 'news',
'orderby' => 'date',
'order' => ASC,
'year' => $the_year_url
);
$cpt_query = new WP_Query( $args );
// the loop
if ( $cpt_query->have_posts() ) : while ( $cpt_query->have_posts() ) : $cpt_query->the_post();
// etc... standard template tags
But here's where I see strange errors.
1. I only get that archive page for 2010. If I click on the 2009 archive link (built by <?php wp_get_archives('type=yearly'); ?>) I get my 404 error page instead.
2. I only get that archive page AT ALL if I have at least one post (of post_type = post) stay published.
So thinking maybe the problem was with wp_get_archives() and the fact that it is "hacked" with that code mentioned earlier, I tried a different approach to build that archive list. Query the SQL database directly:
<ul>
<?php
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_type = 'news' AND post_status = 'publish' 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>
This build an archive list, and the links it generates appear to be exactly the same as those that <?php wp_get_archives('type=yearly'); ?> generated - i.e.
* 2010 with a URL of http://example.com/2010
* 2009 with a URL of http://example.com/2009
The result on the archive page is the same - I get the same errors.
Perhaps the only real lead I have right now is that if I disable my custom permalinks and use the WP default, both the <?php wp_get_archives('type=yearly'); ?> and sql query generated archive links look like this:
* 2010 with a URL of http://example.com/?m=2010
* 2009 with a URL of http://example.com/?m=2009
"m" would be for month? So that isn't right, is it? The query string variable should be something like "?y=" for year, right? Would that cause the page errors I'm seeing. How would I fix that?
So... I'm at a loss. I've stripped my code down to just the bare essentials for a test case... I've deactivated all plugins, etc. Can't figure out the problem. I must be missing something fundamental here. Any suggestions would be appreciated. I'll continue hammering away, but hope to get some tips here b/c I'm spinning my wheels right now. If you think it would help, I can put up some pastebin code, just let me know.
Thanks!