[wp-hackers] Including custom post types on archive pages

Andrew Nacin wp at andrewnacin.com
Thu Jun 9 11:14:58 UTC 2011

On Thu, Jun 9, 2011 at 3:10 AM, Philip Walton <philip at philipwalton.com>wrote:

> I'm writing a plugin that lists your site's archives in an easy-to-read
> format, grouped by month and year. I'm also adding an option to specify
> which of your site's archives existing post types to include in the archive
> list.
> The problem is when I generate the link to the archives page (e.g.
> example.com/2011), unless the theme is already set up to query those
> custom post types, the user will only see posts of type 'post'.
> My idea was to add query vars when my plugin generates the URL (e.g.
> example.com/2011/?post_type=post,page,book) and then use a filter or hook
> to add the specified post type(s) to the query.
> My initial inclination was to use the 'posts_where' filter and then modify
> the actual SQL statement, but that seems like it could be pretty messy and
> possibly unreliable.
> Can anyone recommend a good way to do this?

On pre_get_posts, do a $query->set( 'post_type', array( 'post', 'page',
'book' ) );

More information about the wp-hackers mailing list