Bird
Raised Fist0
Wordpressframework~5 mins

Archive pages for custom types in Wordpress

Choose your learning style10 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.

Practice

(1/5)
1. What must you set to true when registering a custom post type to enable its archive page in WordPress?
easy
A. show_in_menu
B. has_archive
C. publicly_queryable
D. supports

Solution

  1. Step 1: Understand custom post type registration

    When registering a custom post type, you can set various arguments to control its behavior.
  2. Step 2: Identify the argument for archive pages

    The has_archive argument controls whether WordPress generates an archive page listing all posts of that type.
  3. Final Answer:

    has_archive -> Option B
  4. Quick Check:

    Enable archive page = has_archive [OK]
Hint: Set has_archive true to enable archive pages [OK]
Common Mistakes:
  • Confusing has_archive with show_in_menu
  • Forgetting to set has_archive at all
  • Thinking supports controls archive pages
2. Which template file name will WordPress use to display the archive page for a custom post type named book?
easy
A. archive-book.php
B. single-book.php
C. page-book.php
D. book-archive.php

Solution

  1. Step 1: Know WordPress template hierarchy for archives

    WordPress looks for archive-{post_type}.php to display archive pages of custom post types.
  2. Step 2: Match the custom post type name

    For a post type named book, the template file is archive-book.php.
  3. Final Answer:

    archive-book.php -> Option A
  4. Quick Check:

    Archive template = archive-{post_type}.php [OK]
Hint: Use archive-{post_type}.php for custom archives [OK]
Common Mistakes:
  • Using single-book.php which is for single posts
  • Naming file book-archive.php which is invalid
  • Confusing page templates with archive templates
3. Given this code snippet in archive-movie.php for a custom post type movie:
<?php if ( have_posts() ) : ?>
  <ul>
  <?php while ( have_posts() ) : the_post(); ?>
    <li><?php the_title(); ?></li>
  <?php endwhile; ?>
  </ul>
<?php else : ?>
  <p>No movies found.</p>
<?php endif; ?>
What will this archive page display if there are 3 published movie posts titled "A", "B", and "C"?
medium
A.
  • A
  • B
  • C
B.

No movies found.

C. A single movie post content
D. An error because archive-movie.php is missing

Solution

  1. Step 1: Understand the loop in archive template

    The code loops through all posts of the custom type movie and lists their titles in an unordered list.
  2. Step 2: Apply to 3 published posts

    Since there are 3 movies titled "A", "B", and "C", the loop outputs each title as a list item.
  3. Final Answer:

    <ul><li>A</li><li>B</li><li>C</li></ul> -> Option A
  4. Quick Check:

    Loop lists all posts titles [OK]
Hint: Loop outputs all posts titles in archive [OK]
Common Mistakes:
  • Thinking it shows single post content
  • Assuming no posts found when posts exist
  • Confusing archive template with single post template
4. You registered a custom post type album with has_archive set to false. You created archive-album.php but visiting /album/ shows a 404 error. What is the likely fix?
medium
A. Flush rewrite rules by visiting Permalinks settings
B. Rename archive-album.php to single-album.php
C. Set has_archive to true when registering the post type
D. Change post type name to 'albums'

Solution

  1. Step 1: Check has_archive setting

    If has_archive is false, WordPress does not create archive URLs for that post type, causing 404 on archive URL.
  2. Step 2: Enable archive by setting has_archive true

    Changing has_archive to true enables the archive page and makes /album/ URL work.
  3. Final Answer:

    Set has_archive to true -> Option C
  4. Quick Check:

    Enable archive = has_archive true [OK]
Hint: Enable has_archive true to fix 404 on archive URL [OK]
Common Mistakes:
  • Only renaming template file without enabling archive
  • Not flushing rewrite rules after changes
  • Changing post type name unnecessarily
5. You want to create an archive page for a custom post type event that only shows upcoming events (with a custom field event_date in YYYY-MM-DD format). Which approach correctly filters the archive to show only future events?
hard
A. Create a custom page template and manually query all events without filtering
B. Rename archive-event.php to single-event.php and add filtering there
C. Set has_archive to false and use a shortcode to list events
D. Use pre_get_posts action to modify the main query on the archive page filtering by meta_query comparing event_date >= today

Solution

  1. Step 1: Understand filtering archive queries

    To filter archive pages, you hook into pre_get_posts to adjust the main query before it runs.
  2. Step 2: Use meta_query to filter by custom field

    You add a meta_query comparing event_date to today's date to show only upcoming events.
  3. Step 3: Confirm other options are incorrect

    Other options either disable archive, do not filter, or misuse template files.
  4. Final Answer:

    Use pre_get_posts with meta_query filtering event_date >= today -> Option D
  5. Quick Check:

    Filter archive with pre_get_posts + meta_query [OK]
Hint: Filter archive queries with pre_get_posts and meta_query [OK]
Common Mistakes:
  • Trying to filter inside single post template
  • Disabling archive and using shortcode instead
  • Not hooking into pre_get_posts for filtering
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard