2

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

5

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');
7
  • 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) Commented Sep 14, 2011 at 16:18
  • Still note sure if I got your Q (and what's your fn is supposed to do), but see update.
    – kaiser
    Commented Sep 14, 2011 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
    Commented Jun 27, 2012 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
    Commented Jun 27, 2012 at 19:07
  • @kaiser really appreciate you making that - I'll make those modifications and see where I am with that. Thanks again!
    – Zach
    Commented Jun 27, 2012 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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