Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I expected that wp3.1 with the support for archive-{post_type}.php would solve my problems. However, I am still struggling to get it working.

What I want is: a custom date-based archive page per post-type, which displays years and months. Once you click on a year of a month, you'll go to another archive page which outputs the relevant posts.

First of all: I am clueless on how to create a date-based archive page for certain post-types anyway. Is there a certain page-template which automatically gets called when going to {post-type}/archive? <?php wp_get_archives(); ?> doesn't seem post-type aware.

Second of all my date-based permalinks doen't work. A simple {post-type}/2010/01 gives an error404. I've created all my archive-.php pages with simple content:

get_header(); ?>

        <section id="content" class="hfeed">
<?php get_template_part('posts'); ?>
        </section>

<?php get_footer(); ?>

Any clue?

share|improve this question
    
date.php is in place too btw –  grrrbytes Feb 25 '11 at 2:21
    
adding rewrite rules also doesn't work: $newrules['([^/]+)/([^/]+)/([0-9]{4})$'] = 'index.php?post_type=$matches[2]&year=$matches[3]'; $newrules['([^/]+)/([^/]+)/([0-9]{4})/([0-9]{2})$'] = 'index.php?post_type=$matches[2]&year=$matches[3]&month=$matches[4]'; –  grrrbytes Feb 25 '11 at 2:30

2 Answers 2

Did you add 'has_archive => 'my_slug' to your register_post_type() arguments? That should at least help you with your second problem.

share|improve this answer

The problem I think you are having is that WordPress doesn't touch its built in tags such as date and postname when you are using custom post types. Try using this code, which is found here:

/ Add filter to plugin init function
add_filter('post_type_link', 'translate_permalink', 10, 3); 
// Adapted from get_permalink function in wp-includes/link-template.php
function translate_permalink($permalink, $post_id, $leavename) {
    $post = get_post($post_id);
    $rewritecode = array(
        '%year%',
        '%monthnum%',
        '%day%',
        '%hour%',
        '%minute%',
        '%second%',
        $leavename? '' : '%postname%',
        '%post_id%',
        '%category%',
        '%author%',
        $leavename? '' : '%pagename%',
    );

    if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
        $unixtime = strtotime($post->post_date);

        $category = '';
        if ( strpos($permalink, '%category%') !== false ) {
            $cats = get_the_category($post->ID);
            if ( $cats ) {
                usort($cats, '_usort_terms_by_ID'); // order by ID
                $category = $cats[0]->slug;
                if ( $parent = $cats[0]->parent )
                    $category = get_category_parents($parent, false, '/', true) . $category;
            }
            // show default category in permalinks, without
            // having to assign it explicitly
            if ( empty($category) ) {
                $default_category = get_category( get_option( 'default_category' ) );
                $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
            }
        }

        $author = '';
        if ( strpos($permalink, '%author%') !== false ) {
            $authordata = get_userdata($post->post_author);
            $author = $authordata->user_nicename;
        }

        $date = explode(" ",date('Y m d H i s', $unixtime));
        $rewritereplace =
        array(
            $date[0],
            $date[1],
            $date[2],
            $date[3],
            $date[4],
            $date[5],
            $post->post_name,
            $post->ID,
            $category,
            $author,
            $post->post_name,
        );
        $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    } else { // if they're not using the fancy permalink option
    }
    return $permalink;
}

This is a filter that is normally built in and called into Wordpress when dealing with posts but doesn't get called for custom post types. Basically, it handles tags that are used with permalinks. This code should let you use dates for your CPT.

share|improve this answer

Your Answer

 
discard

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

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