Multiple post types in archives (filter?)

I’m trying to get all custom post types to appear in my archive sections on a site, including monthly, tagged, author archives. At the moment, I’ve thought of something like:

add_filter('pre_get_posts', 'sw_custom_post_type_includes');

function sw_custom_post_type_includes($query) {

        $post_type = array('post','custom1','custom2','custom3');

        $query->set('post_type',$post_type);

        return $query;

}

But I don’t want to use pre_get_posts, as this messes with all queries. Is there a filter for, in effect, pre_get_archives? Can’t seem to find a solution on Google.

1 Answer
1

Try this:

function sw_custom_post_type_includes($query) {
        $post_types = array('post','custom1','custom2','custom3');
        if ( ! is_archive() && ! in_array( get_post_type(), $post_types ) )
                return $query;

        $query->set( 'post_type', $post_types );

        return $query;
}
add_filter('pre_get_posts', 'sw_custom_post_type_includes');

Leave a Comment