0

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?

2

1 Answer 1

0

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 :)

1
  • 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, 2011 at 22:16

Your Answer

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