Add Custom Post Types to Archive

If your Custom Post Types are not showing up in your archive.php or category.php templates (even with ‘has_archive’ => true enabled within the register_post_type function), you may need to do some additional theme adjustments!

Use this function to display the archives.

Original:

function wpa_cpt_tags( $query ) {
    if ( $query->is_tag() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'object' ) );
    }
}
add_action( 'pre_get_posts', 'wpa_cpt_tags' );

Modified:

The comment on the source link describes a way to include all categories within the function, shown below.

function wpa_cpt_tags( $query ) {
    if ( $query->is_tag() || $query->is_category() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'custom_post_type' ) );
    }
}
add_action( 'pre_get_posts', 'wpa_cpt_tags' );

Sourced here.

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.