• In the code below I can create a Book custom Type successfully.
    But I’m not able to add a date archive, for example: http://mysite.com/book/2013.
    Using this rule I get error. What I need to add to the code to get a year/month/day archive?

    <?php
    /*
    Plugin Name: Book Post Type
    Plugin URI: http://mysite.it
    Description: A Rewrite Test
    Version: 0.1
    Author: Giovanni Putignano
    Author URI: http://mysite.it
    Licence: GPLv2
    */
    
    register_activation_hook( __FILE__, 'gp_plugin_activation' );
    
    function gp_plugin_activation() {
        gp_register_post_type();
        flush_rewrite_rules();
    }
    
    register_deactivation_hook( __FILE__, 'gp_plugin_deactivation' );
    
    function gp_plugin_deactivation() {
        flush_rewrite_rules();
    }
    
    add_action( 'init', 'gp_register_post_type' );
    
    function gp_register_post_type() {
        $argsBooks   = array(
            'label'                => 'Books',
            'description'           => 'A Books Post Type',
            'public'                => true,
            'show_ui'               => true,
            'has_archive'           => false,
            'show_in_nav_menus'     => true,
            'show_in_menu'          => true,
            'show_in_admin_bar'     => true,
            'menu_position'         => 5,
            'capability_type'       => 'post',
            'hierarchical'          => true,
            'supports'              => array( 'title', 'editor' ),
            'taxonomies'            => array( 'category', 'post_tag' ),
            'rewrite'               => false
        );
    
        gp_add_permastructs();
        register_post_type( 'book', $argsBooks );
    }
    
    function gp_add_permastructs() {
        add_permastruct( 'book', 'book/%book%.html' );
    }
    
    add_filter( 'post_type_link', 'gp_post_type_link', 10, 3 );
    
    function gp_post_type_link($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;
    }
    ?>
Viewing 1 replies (of 1 total)
  • I didn’t go in depth through your code, but I thought I’d just ask: do you have a template page for the CPT archive? Typically you’d want to create a page called, in your case, archive-book.php. Copy your archive.php file from your theme (or from TwentyEleven) and see if that does it. You may not need a lot more code to govern archives if that’s set up correctly.

    Separately: My understanding is archives are by month/year but not by day, even when the permalinks for posts are by date – but I may be wrong about that default behavior. Even if it is the case, I’m sure you could change it to include day if that’s what you need.

Viewing 1 replies (of 1 total)

The topic ‘Custom Post Type Date Archive’ is closed to new replies.