ethitter

Date-based archives for WordPress' custom post types

Oct 23rd, 2011
106
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function action_generate_rewrite_rules( $wp_rewrite ) {
  2.     $rules = array();
  3.  
  4.     //Replace POST TYPES HERE in the array below with names of the custom post types that need date-based archives.
  5.  
  6.     foreach( array( POST TYPES HERE ) as $post_type ) {
  7.         $post_type_object = get_post_type_object( $post_type );
  8.        
  9.         $post_type_slug = '';
  10.        
  11.         if( $post_type_object->rewrite[ 'with_front' ] && $wp_rewrite->front != '/' )
  12.             $post_type_slug .= $wp_rewrite->front;
  13.        
  14.         $post_type_slug .= $post_type_object->rewrite[ 'slug' ];
  15.        
  16.         $post_type_slug = path_join( $post_type_slug, 'date' );
  17.        
  18.         $rules = array_merge( $rules, array(
  19.             $post_type_slug . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=' . $post_type . '&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]',
  20.             $post_type_slug . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=' . $post_type . '&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]',
  21.             $post_type_slug . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$' => 'index.php?post_type=' . $post_type . '&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]',
  22.             $post_type_slug . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$' => 'index.php?post_type=' . $post_type . '&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
  23.             $post_type_slug . '/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=' . $post_type . '&year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]',
  24.             $post_type_slug . '/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=' . $post_type . '&year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]',
  25.             $post_type_slug . '/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$' => 'index.php?post_type=' . $post_type . '&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]',
  26.             $post_type_slug . '/([0-9]{4})/([0-9]{1,2})/?$' =>  'index.php?post_type=' . $post_type . '&year=$matches[1]&monthnum=$matches[2]',
  27.             $post_type_slug . '/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=' . $post_type . '&year=$matches[1]&feed=$matches[2]',
  28.             $post_type_slug . '/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=' . $post_type . '&year=$matches[1]&feed=$matches[2]',
  29.             $post_type_slug . '/([0-9]{4})/page/?([0-9]{1,})/?$' => 'index.php?post_type=' . $post_type . '&year=$matches[1]&paged=$matches[2]',
  30.             $post_type_slug . '/([0-9]{4})/?$' => 'index.php?post_type=' . $post_type . '&year=$matches[1]'
  31.         ) );
  32.     }
  33.    
  34.     $wp_rewrite->rules = $rules + $wp_rewrite->rules;
  35. }
RAW Paste Data