Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

How to Include Custom Post Types (CPT) in WordPress Archive? Show in Category and Tag pages

Have you recently created WordPress Custom Post Type? But your newly published Custom Posts are not appearing in your WordPress Archive page? i.e. Category and Tag archive page?

Same thing happened to me today. We have created new Custom Post Type called Guides recently. Here is a detailed tutorial if you are looking for it.

After publishing our first post when I clicked on Category and Tag links, Custom Post wasn’t appearing in Archive.php page.

Take a look here:

Crunchify's Post section with Categories and Tags

Here is after post section of our Crunchify’s post. Just click on Category or Tags link.

Before adding Archive support here is how you would see Archive.php page as below.

Custom Post Type and Missing Custom Post in Archive.php page

How to fix missing this issue? Do you have any of below questions?

  • Displaying category archive of custom post types in WordPress
  • Custom post type taxonomy “tag” archive : no post found. How to fix?
  • On my archives page, I can’t see my custom post types. How to fix?
  • Custom Post Type Taxonomies – Posts not showing in Category or Tag pages
  • Custom Post Type Not Displaying Properly on Category/Tag pages
  • Category not showing posts made with CPT
  • Add Custom Post Type To Tag And Category Archive Pages
  • How To Add Custom Content To WordPress Category Archives
  • How to Include Custom Post Types in WordPress Archive Pages
  • Make Archives.php Include Custom Post Types

You need to add below code to your theme’s functions.php file

Here is how you need to add CPT’s Archive.php support.

function crunchify_add_cpt_to_archive_page( $query ) {
  if( (is_category() || is_tag()) && $query->is_archive() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'guides'
        ));
    }
    return $query;
}
add_filter( 'pre_get_posts', 'crunchify_add_cpt_to_archive_page' );

Once you save it, you are done. Just refresh page and now you will see Custom Post type in your Category and Archive page.

Custom Post Type appears in Archive.php page after modifying wp_query and pre_get_posts

Let us know if you face any issue showing your Custom Post Type on your Archive page.

The post How to Include Custom Post Types (CPT) in WordPress Archive? Show in Category and Tag pages appeared first on Crunchify.

Enregistrer un commentaire

0 Commentaires