How do I create an archive for a CPT

56 Views Asked by At

I have created a CPT (Custom Post Type) in Wordpress called "minutes" and am using the following php to give the post an archive

add_filter( 'getarchives_where', 'getarchives_where_filter', 10, 2 );
add_filter( 'generate_rewrite_rules', 'generate_events_rewrite_rules' );

function getarchives_where_filter( $where, $args ) {

    if ( isset($args['post_type']) ) {      
        $where = "WHERE post_type = '$args[post_type]' AND post_status = 'publish'";
    }

    return $where;
}

function generate_minutes_rewrite_rules( $wp_rewrite ) {

    $event_rules = array(
        'minutes/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$' => 'index.php?post_type=minutes&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
        'minutes/([0-9]{4})/([0-9]{1,2})/?$' => 'index.php?post_type=minutes&year=$matches[1]&monthnum=$matches[2]',
        'minutes/([0-9]{4})/?$' => 'index.php?post_type=minutes&year=$matches[1]' 
    );

    $wp_rewrite->rules = $minutes_rules + $wp_rewrite->rules;
}

function get_archives_minutes_link( $link ) {

    return str_replace( get_site_url(), get_site_url() . '/minutes', $link );

};

and to create a monthly side bar on my single post template. I am using this:

add_filter( 'get_archives_link', 'get_archives_minutes_link', 10, 2 );

wp_get_archives( array( 'post_type' => 'minutes' ) );            
wp_get_archives( array( 'post_type' => 'minutes', 'type' => 'yearly' ) );
wp_get_archives( array( 'post_type' => 'minutes', 'type' => 'monthly' ) );
wp_get_archives( array( 'post_type' => 'minutes', 'type' => 'daily' ) );

remove_filter( 'get_archives_link', 'get_archives_minutes_link', 10, 2 );

I have resaved the permalinks but the monthly links dont link to the archive.

any ideas?

2

There are 2 best solutions below

0
Marsellus On

https://developer.wordpress.org/themes/basics/template-hierarchy/#custom-post-types

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'minutes',
        array(
            'labels' => array(
                'name' => __( 'Minutes' ),
                'singular_name' => __( 'Minute' )
            ),
        'public' => true,
        'has_archive' => true,
        )
    );
}

Make sure 'has_archive' => true

Then, create an archive-minutes.php file. This will be the archive for the minutes page.

In that page, use the default wordpress loop, or whatever you want to get the content

if ( have_posts() ) :

    /* Start the 'WORDPRESS LOOP' */
    while ( have_posts() ) :
        
        /* Might be one or the other vased on your setup. */
        the_post();
        get_post_type();

    endwhile;

else :

    echo 'No Minutes CPT posts created yet.';

endif;
0
Dishant On

It appears like this question is somewhat similar to what you need. Please check below answer and update your code for "minutes" post type.

Custom post type yearly/ monthly archive

Hopefully, you will get your issue resolved with this. I am not adding the same code again here.