Archive pages show a list of posts for a specific custom type. They help visitors find all items of that type in one place.
Archive pages for custom types in 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.
/books/.register_post_type('book', [ 'label' => 'Books', 'public' => true, 'has_archive' => true, 'rewrite' => ['slug' => 'books'], ]);
/films/ instead of /movie/.register_post_type('movie', [ 'label' => 'Movies', 'public' => true, 'has_archive' => 'films', 'rewrite' => ['slug' => 'films'], ]);
register_post_type('event', [ 'label' => 'Events', 'public' => true, 'has_archive' => false, ]);
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.
<?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(); ?>
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.
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
true when registering a custom post type to enable its archive page in WordPress?Solution
Step 1: Understand custom post type registration
When registering a custom post type, you can set various arguments to control its behavior.Step 2: Identify the argument for archive pages
Thehas_archiveargument controls whether WordPress generates an archive page listing all posts of that type.Final Answer:
has_archive-> Option BQuick Check:
Enable archive page = has_archive [OK]
- Confusing has_archive with show_in_menu
- Forgetting to set has_archive at all
- Thinking supports controls archive pages
book?Solution
Step 1: Know WordPress template hierarchy for archives
WordPress looks forarchive-{post_type}.phpto display archive pages of custom post types.Step 2: Match the custom post type name
For a post type namedbook, the template file isarchive-book.php.Final Answer:
archive-book.php-> Option AQuick Check:
Archive template = archive-{post_type}.php [OK]
- Using single-book.php which is for single posts
- Naming file book-archive.php which is invalid
- Confusing page templates with archive templates
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"?Solution
Step 1: Understand the loop in archive template
The code loops through all posts of the custom typemovieand lists their titles in an unordered list.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.Final Answer:
<ul><li>A</li><li>B</li><li>C</li></ul> -> Option AQuick Check:
Loop lists all posts titles [OK]
- Thinking it shows single post content
- Assuming no posts found when posts exist
- Confusing archive template with single post template
album with has_archive set to false. You created archive-album.php but visiting /album/ shows a 404 error. What is the likely fix?Solution
Step 1: Check has_archive setting
Ifhas_archiveis false, WordPress does not create archive URLs for that post type, causing 404 on archive URL.Step 2: Enable archive by setting has_archive true
Changinghas_archiveto true enables the archive page and makes/album/URL work.Final Answer:
Sethas_archivetotrue-> Option CQuick Check:
Enable archive = has_archive true [OK]
- Only renaming template file without enabling archive
- Not flushing rewrite rules after changes
- Changing post type name unnecessarily
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?Solution
Step 1: Understand filtering archive queries
To filter archive pages, you hook intopre_get_poststo adjust the main query before it runs.Step 2: Use meta_query to filter by custom field
You add a meta_query comparingevent_dateto today's date to show only upcoming events.Step 3: Confirm other options are incorrect
Other options either disable archive, do not filter, or misuse template files.Final Answer:
Use pre_get_posts with meta_query filtering event_date >= today -> Option DQuick Check:
Filter archive with pre_get_posts + meta_query [OK]
- Trying to filter inside single post template
- Disabling archive and using shortcode instead
- Not hooking into pre_get_posts for filtering
Ready to prove it?
Pass the test and this pattern moves unknown
