Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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');
share|improve this answer
    
Hey, I tried something like this and it interferes with all queries on the archive page. Including things in the sidebar, the wordpress native menu system, it all seems to get overridden. Any way around that? (I take it there isn't an archive specific filter) –  Michael Watson Sep 14 '11 at 16:18
    
Still note sure if I got your Q (and what's your fn is supposed to do), but see update. –  kaiser Sep 14 '11 at 17:46
    
@kaiser Using a slightly modified version of your code you posted above, but post_type=page is not working (it returns all of the results again) yet every other post type works as intended. I don't believe this is due to anything I added (basically I just pulled in all of the public post types in an array) -- have any thoughts on why it's not picking up on that? –  Zach Jun 27 '12 at 18:17
1  
@Zach Take a look at the comments of this pastebin. Copypasta will never work out. Always go through stuff character by character, line by line. And always comment what stuff should do, before writing it. This way you won't loose your mind and it's easier to check. –  kaiser Jun 27 '12 at 19:07
    
@kaiser really appreciate you making that - I'll make those modifications and see where I am with that. Thanks again! –  Zach Jun 27 '12 at 21:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.