WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I downloaded a widget for my blog, which works just like the calendar widget but displays a custom post type instead. It correctly marks my CPT-posts and creates links, such as http://localhost:8888/wp/?m=201105 for a certain month and http://localhost:8888/wp/?m=20110510 for a certain day.

Now I want to create the required archive template, but it keeps falling back to index.php instead of date.php or archive.php - and the loop in index.php returns empty. I am flabbergasted, has anyone seen this before?

My hypothesis: it returns empty because I does not include custom post types. It does not explain why date.php / archive.php isn't being used (and is_date() appears to return false), but it would explain why the loop is empty because I have no other articles yet. If this is the case, how can I include CPTs?

share|improve this question
    
What's the name of this plugin? – onetrickpony Jul 20 '11 at 20:00
    
This is where I got it: pippinsplugins.com/custom-post-type-calendar-widget – user5028 Jul 20 '11 at 20:15
up vote 0 down vote accepted

The problem is that the plugin only handles the calendar part, and not the archive query.

What you need to do:

Change the main query on archive pages if the post_type argument is present, eg: http://localhost:8888/wp/?m=201105&post_type=candy :

add_filter('pre_get_posts', 'atom_include_cpt_in_archives');

function atom_include_cpt_in_archives($query){
  // validate
  if(is_archive() && isset($_GET['post_type']) && post_type_exists($_GET['post_type']))
    $query->set('post_type', $_GET['post_type']);

  return $query;
}

Next, you'll have to make your own get_calendar() version, because the native function is not hookable:

  • add a $post_type argument to the function and change all the db queries:

    WHERE post_type = 'post' to WHERE post_type = $post_type

  • append the current post type that's being requested to get_month_link() / get_day_link():

    add_query_arg('post_type', $post_type, get_month_link($previous->year, $previous->month)) ...

  • maybe override the $post_type function argument with the query argument if it's set:

    if(isset($_GET['post_type']) && post_type_exists($_GET['post_type'])) $post_type = $_GET['post_type'];

I did a calendar widget like this which among other things, supports CPT. I could post the code, but since it's part of a theme framework you'd have to figure it out yourself how to separate it :)

share|improve this answer
    
Thank you very much. Following your instructions it was not hard to modify the widget to make it work. The only thing I had to do was adding the post_type query variable, like this: add_query_arg( 'post_type', str_replace( "'", '', $post_types ), THE LINK ); and it works :) – user5028 Jul 20 '11 at 22:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.