Add custom post types to archives page

CPT ArchivesAfter we have added custom post types snippet into our functions.php we expect that our new articles under the custom post type to appear on archive pages, but they will not. It’s very logical from the developers side, WordPress maintain that sorting archives is for normal post types “posts”, and adding custom post types into categories and post_tags requires some custom code.

So, if you’d like your custom post types to show up in archive listings for your site’s standard tags and categories you can add the following code to your functions.php file:

Add custom post types to archives

[php]
/**
* Snippet Name: Add custom post types to archives page
* Snippet URL: http://wperror.com/add-custom-post-types-to-archives-page.html
*/

function add_custom_types_to_tax( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars[‘suppress_filters’] ) ) {

// Get all your post types
$post_types = get_post_types();

$query->set( ‘post_type’, $post_types );
return $query;
}
}
add_filter( ‘pre_get_posts’, ‘add_custom_types_to_tax’ );
[/php]
Still if you wish to display only specific custom post types into archive pages of tags and categories you can replace the line $post_types = get_post_types(); with $post_types = array( ‘post’, ‘your_custom_type’ );.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>