0 Replies - 948 Views - Last Post: 29 April 2011 - 12:12 PM

#1 kerbeross  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-April 11

How to display archives for custom post types?

Posted 29 April 2011 - 12:12 PM

I'm trying to display an archive listing for all posts (including custom post types) but there is a problem:

Given a link like this: http://siteexample.com/2011/04/ I get a 404 not found page if there are only custom posts to show and no normal posts.

This is because, by default, wordpress looks only for normal posts. I tried this:

add_filter( 'getarchives_where' , 'ucc_getarchives_where_filter' , 10 , 2 );
function ucc_getarchives_where_filter( $where , $r ) {        
  $args = array(
    'public' => true ,
    '_builtin' => false
  );
  $output = 'names';
  $operator = 'and';

  $post_types = get_post_types( $args , $output , $operator );
  $post_types = array_merge( $post_types , array( 'post' ) );
  $post_types = "'" . implode( "' , '" , $post_types ) . "'";
  return str_replace( "post_type = 'post'" , "post_type IN ( $post_types )" , $where );
}



but this only works when using wp_get_archives (I mean when listing the links).

Are there any hooks so I could be able to modify the main query just before I get 404? Any ideas?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1