Paginate a custom post type archive
Custom post types can give a publication its own taxonomy and fields, but its archive still uses WordPress’s main query. The reliable approach is to register the type correctly, let the archive template consume that query, and add pagination through WordPress’s established template functions. The practical WordPress answers collection offers a useful reference when archive URLs, query variables or loops behave differently from a normal post archive.
Before writing the template, decide which posts belong on each page, how readers will move between pages and whether filters must survive navigation. These decisions affect the URL structure, query variables and archive markup. The following approach works with a conventional WordPress theme, a child theme or a small companion plugin, while leaving room for custom taxonomies and WooCommerce integrations.
Register the post type and archive
Register the post type in a small plugin or the functions file of a child theme. The archive slug comes from has_archive, while the rewrite slug controls individual entries. Keeping those values separate makes the archive URL predictable and avoids a mismatch between the public path and the template filename.
add_post_type(
'case_study',
array(
'public' => true,
'has_archive' => 'case-studies',
'rewrite' => array( 'slug' => 'case-studies' ),
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
'show_in_rest' => true,
)
);
The has_archive value can be true or a custom slug. A string such as case-studies produces an archive at /case-studies/. Add suitable labels for the singular and plural names so archive titles do not display a generic value such as “Posts”.
Rewrite rules are generated from the post type registration. After adding or changing the archive slug, visit Settings, Permalinks and save the settings once, or flush rewrite rules during plugin activation. Avoid flushing them on every init request because that adds unnecessary database work. For a post type named case_study, WordPress looks for archive-case_study.php, then falls back to archive.php and finally index.php.
Build the archive template
Create archive-case_study.php in the active child theme. Using the main archive query avoids loading the same posts twice and gives the pagination functions an accurate total page count. The template should also provide a clear empty-state message when no matching entries exist.
<?php
get_header();
?>
<main class="site-main case-study-archive">
<h1>
<?php
echo esc_html( post_type_archive_title( '', false ) );
?>
</h1>
<?php if ( have_posts() ) : ?>
<div class="archive-grid">
<?php
while ( have_posts() ) :
the_post();
?>
<article <?php post_class(); ?>>
<h2>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<?php if ( has_excerpt() ) : ?>
<p><?php the_excerpt(); ?></p>
<?php endif; ?>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No case studies have been published yet.</p>
<?php endif; ?>
</main>
<?php
get_footer();
?>
A second WP_Query inside this template would duplicate the main loop and could produce confusing page totals. Adjust the main query with pre_get_posts instead. This keeps the archive request limited to the relevant post type and gives administrators a single place to control ordering and the number of entries per page.
function case_study_archive_settings( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->is_post_type_archive( 'case_study' ) ) {
$query->set( 'posts_per_page', 6 );
$query->set( 'orderby', 'date' );
$query->set( 'order', 'DESC' );
}
}
add_action( 'pre_get_posts', 'case_study_archive_settings' );
The conditional limits the changes to the public archive. Related searches, administration screens and other archives retain their normal query settings. The callback should remain in a child theme or plugin so an active parent theme update does not remove it.
Add pagination that follows the query
Place WordPress’s pagination function inside the have_posts() condition and after the loop. It reads the current page and total number of pages directly from the main query, so it remains consistent with the posts displayed by the template.
<?php
the_posts_pagination(
array(
'mid_size' => 2,
'end_size' => 1,
'prev_text' => __( 'Previous page', 'case-study-theme' ),
'next_text' => __( 'Next page', 'case-study-theme' ),
'screen_reader_text' => __( 'Case study pages', 'case-study-theme' ),
)
);
);
Custom post type archives normally use the paged query variable. The archive URL may appear as /case-studies/, /case-studies/page/2/ or use ?paged=2, depending on the permalink configuration. The page variable is mainly associated with paginated content on a static front page and should not replace paged for this archive.
A genuinely separate query requires its own pagination calculation. Keep no_found_posts disabled because max_num_pages depends on the total number of matching posts.
$paged = max( 1, absint( get_query_var( 'paged' ) ) );
$archive_query = new WP_Query(
array(
'post_type' => 'case_study',
'posts_per_page' => 6,
'paged' => $paged,
)
);
Generate links with paginate_links() and calculate the total from that query. Calling wp_reset_postdata() after a secondary loop restores the original post data for any following template code.
echo paginate_links(
array(
'base' => str_replace(
999999999,
'%#%',
esc_url( get_pagenum_link( 999999999 ) )
),
'format' => '',
'current' => $paged,
'total' => $archive_query->max_num_pages,
'type' => 'list',
)
);
Preserve filters and archive URLs
Filtered archives need a registered public query variable. For example, a client taxonomy can be filtered through a GET form without changing the archive path. Register the variable, read it with get_query_var() and add a taxonomy condition to the existing main-query callback.
add_filter(
'query_vars',
function ( $vars ) {
$vars[] = 'client';
return $vars;
}
);
Inside the pre_get_posts callback, use the sanitised value to build an allowlisted taxonomy query.
$client = sanitize_title( get_query_var( 'client' ) );
if ( $client ) {
$query->set(
'tax_query',
array(
array(
'taxonomy' => 'client',
'field' => 'slug',
'terms' => array( $client ),
),
)
);
}
The archive form should submit to get_post_type_archive_link( 'case_study' ). Populate a taxonomy dropdown with get_terms(), preserve the selected slug and use a GET field named client. Search fields can use the standard s parameter, while get_search_query() safely restores the submitted search phrase.
Pagination links must retain these filters. get_pagenum_link() and paginate_links() preserve the current query string when their base URL is generated correctly. Manually constructed links should use add_query_arg() and remove_query_arg() rather than concatenating strings, which helps prevent lost filters and malformed ampersands.
Each paginated URL should have a useful title, such as “Case Studies – Page 2”. Avoid canonicalising every page to page one, since that can collapse distinct result sets into one URL. An SEO plugin can generate pagination metadata, while the theme remains responsible for semantic headings, descriptive link text and an accessible navigation region.
Support WooCommerce and Australian conditions
WooCommerce already registers product as a post type and supplies its own shop archive, loop and pagination. A normal product listing should use WooCommerce templates and woocommerce_after_shop_loop rather than a custom archive template. Calling the_posts_pagination() inside the standard WooCommerce shop can create duplicate navigation controls.
The WooCommerce pagination arguments can be adjusted through its own filter.
add_filter(
'woocommerce_pagination_args',
function ( $args ) {
$args['mid_size'] = 2;
return $args;
}
);
A separate custom post type can still accompany WooCommerce, such as project case studies that reference purchased products. It should query published case studies rather than WooCommerce customer records, and product prices or stock states should come from WooCommerce functions. Custom ordering, cart actions and stock filtering require WooCommerce-compatible hooks because WooCommerce does not automatically apply its product behaviour to unrelated post types.
Many Australian retailers use WooCommerce with Australian dollars and GST-inclusive consumer prices. Where GST applies, Australian Consumer Law requires displayed consumer prices to be clear and generally include the tax. Archive cards that expose prices should therefore use WooCommerce’s price output instead of calculating tax totals independently.
WordPress also needs the correct Australian timezone. Sites serving Sydney or Melbourne can use Australia/Sydney or Australia/Melbourne, which observe daylight saving, while Brisbane uses Australia/Brisbane and remains on a fixed offset. Archive dates should follow the site timezone even when the host records server logs in UTC. A .com.au domain is common in the Australian market, and archive pagination inherits that domain without requiring special code.
Public case studies may contain client names, photographs or testimonials. Where the Privacy Act 1988 applies, handling personal information should align with the Australian Privacy Principles, including appropriate notices, consent and access controls. Archive templates should query published content and avoid displaying private customer fields, draft material or internal WordPress metadata.
Troubleshoot common archive failures
A 404 usually means that has_archive has not been enabled, the rewrite rules predate the post type registration or the template filename does not match the post type name. Confirm the archive URL, inspect has_archive and flush rewrite rules once. For case_study, place archive-case_study.php in the child theme root and avoid renaming it to archive-case-studies.php, because the template name uses the post type key.
Repeated entries or an archive that always shows page one often comes from using page instead of paged, changing the main query after pagination has been calculated or running multiple loops with different parameters. Check get_query_var( 'paged' ), verify that pre_get_posts affects only the intended archive and inspect max_num_pages. A secondary query using no_found_posts => true cannot provide an accurate total, while page caching can conceal query changes until its cache is cleared.
Finally, sanitise every public query variable, allowlist permitted taxonomy values and escape all output. Limit custom filters to known query variables so arbitrary input cannot influence unrelated queries. Theme styles for .navigation.pagination determine whether the controls remain visible, and WooCommerce pages need WooCommerce’s pagination styles instead. Browser developer tools, PHP error logs and WordPress debug logging can identify missing templates, malformed URLs and plugin conflicts without exposing site data. This combination produces an archive that remains navigable, cacheable and secure across every page.