Get Pages to Show Up in Category & Tag Archives

In a previous post we shared how to Add Categories & Tags to WordPress Pages. But what good is that if WordPress doesn’t include pages into the category and tag archive pages. Depending on your specific implementation, not very much.

Here is a quick bit of code that we found at bajada.net that will add those pages that you have categorized or tagged into the appropriate archives.


add_filter( 'pre_get_posts' , 'ucc_include_custom_post_types' );
function ucc_include_custom_post_types( $query ) {
    global $wp_query;

    /* Don't break admin or preview pages. */
    if ( !is_preview() && !is_admin() && !is_page() && !is_single() && !is_home() ) {
        $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', 'page' ) );

        if ($query->is_feed) {
            // Do feed processing here.
        } else {
            $my_post_type = get_query_var( 'post_type' );
            if ( empty( $my_post_type ) )
                $query->set( 'post_type' , $post_types );
        }
    }

    return $query;

}

Line 6 is kind of important. Here you can use Conditional Tags to prevent pages from being added under those conditions. For instance, we’ve added !is_home() because although we may want pages in our archive pages, we didn’t want them in our main blog index. Have fun.