Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date archives for custom post type

I have seen many questions/posts regarding this, but have yet to find a decent solution. Basically I am trying to do what wp_get_archives does, but for a custom post type (personally I am unsure why wp_get_archives doesn't support custom post types!).

The code I am currently using is as follows

functions.php

function Cpt_getarchives_where_filter( $where , $r ) {
  $post_type = 'events';
  return str_replace( "post_type = 'post'" , "post_type = '$post_type'" , $where );
}

sidebar-events.php

add_filter( 'getarchives_where' , 'Cpt_getarchives_where_filter' , 10 , 2 );
wp_get_archives();
remove_filter('getarchives_where' , 'Cpt_getarchives_where_filter' , 10 );

This code displays the dates (e.g. April 2014, March 2014) etc, which is great, but clicking the links just goes to a 404. The URL that is created on each date link is /2014/04/, however it should be something like /events/2014/04/.

Is there any way to include 'events' in the URL so that the archive-events.php template can be used, and is there any reason why the links currently generate a 404?

Many thanks for any help

like image 871
wickywills Avatar asked Apr 30 '14 11:04

wickywills


People also ask

How do I create a custom post type archive page?

Let's start by adding the Posts block to display items from your custom post type. Simply drag and drop the Posts block in the Advanced section onto your page. By default, the posts block will display your blog posts. Click on the block settings and then select your post type under Query by Post Type section.

What are date archives in WordPress?

Date Archives: This WordPress archive lists all the posts published on a specific date, month or year. For example, if you go to https://aioseo.com/2020/ you'll see all posts we've published in 2020. These archives may be important if you run a news website where visitors want to find posts by date.

Where are custom post types stored?

Custom Post Type Definition A single item of such content is generally called a post, although a post is also a specific post type. Internally, all the post types are stored in the same place, in the wp_posts database table, but are differentiated by a column called post_type.

Can I assign a template to a custom post type?

You can simply create and assign custom page templates for your custom post type in your custom plugin. Just create 2 template file - single-{post_type}.


1 Answers

There are many examples on the Internet similar as your, but the problem is that although wp_get_archive() will create a list of custom post type archives, the links still points to the default post type. This is because Wordpress do not generate rewrite rules for the archives of the custom post type, you will have to manually create them. Here is an example of how to generate rules for yearly, monthly, and daily archives. It also shows how to convert links with get_archives_link filter. Be sure to add 'has_archive' => true to register_post_type() array of arguments and to flush the rewrite rules by visiting the settings->permalinks page in admin.

functions.php

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_events_rewrite_rules( $wp_rewrite ) {

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

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

function get_archives_events_link( $link ) {

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

};

sidebar.php examples

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

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

remove_filter( 'get_archives_link', 'get_archives_events_link', 10, 2 );
like image 50
Danijel Avatar answered Sep 30 '22 13:09

Danijel