0
0
Wordpressframework~5 mins

Archive pages for custom types in Wordpress

Choose your learning style9 modes available
Introduction

Archive pages show a list of posts for a specific custom type. They help visitors find all items of that type in one place.

You created a custom post type like 'Books' and want a page listing all books.
You want to organize content by type and show all entries on one archive page.
You want visitors to browse all items of a custom type easily.
You want to customize how the list of custom type posts looks.
You want WordPress to automatically generate a page for your custom type.
Syntax
Wordpress
register_post_type('custom_type', [
  'label' => 'Custom Type',
  'public' => true,
  'has_archive' => true,
  'rewrite' => ['slug' => 'custom-type'],
  // other args
]);

Set has_archive to true to enable archive pages.

The rewrite slug controls the URL for the archive page.

Examples
This creates a 'Books' custom type with an archive page at /books/.
Wordpress
register_post_type('book', [
  'label' => 'Books',
  'public' => true,
  'has_archive' => true,
  'rewrite' => ['slug' => 'books'],
]);
This creates a 'Movies' type with archive URL /films/ instead of /movie/.
Wordpress
register_post_type('movie', [
  'label' => 'Movies',
  'public' => true,
  'has_archive' => 'films',
  'rewrite' => ['slug' => 'films'],
]);
This disables archive pages for 'Events'. No archive URL will be created.
Wordpress
register_post_type('event', [
  'label' => 'Events',
  'public' => true,
  'has_archive' => false,
]);
Sample Program

This code registers a 'Books' custom post type with an archive page at /books/. The archive-book.php template lists all book posts with links.

Wordpress
<?php
// Register custom post type with archive
function my_custom_post_type() {
  register_post_type('book', [
    'label' => 'Books',
    'public' => true,
    'has_archive' => true,
    'rewrite' => ['slug' => 'books'],
    'supports' => ['title', 'editor'],
  ]);
}
add_action('init', 'my_custom_post_type');

// Template file: archive-book.php
// Place this in your theme folder to customize archive page
?>

<?php get_header(); ?>
<h1>Books Archive</h1>
<?php if (have_posts()) : ?>
  <ul>
  <?php while (have_posts()) : the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
  <?php endwhile; ?>
  </ul>
<?php else : ?>
  <p>No books found.</p>
<?php endif; ?>
<?php get_footer(); ?>
OutputSuccess
Important Notes

Flush rewrite rules after registering a new post type by visiting Settings > Permalinks or calling flush_rewrite_rules() once.

Archive template files follow the pattern archive-{post_type}.php to customize each archive page.

If no custom archive template exists, WordPress falls back to archive.php or index.php.

Summary

Set has_archive to true when registering a custom post type to enable archive pages.

Use archive-{post_type}.php template files to customize archive page layout.

Archive pages list all posts of that custom type for easy browsing.