When managing a content-rich WordPress website, you might encounter a need to tweak the main query on specific pages or archives, particularly when dealing with custom post types. The PHP snippet below offers a solution to alter the post query for custom post type archives to display all posts without pagination.
The Code
<?php
function pre_get_posts($query) {
// Check if we're not in the admin area, it's the main query, and we're on the custom post type archive page
if (!is_admin() && $query->is_main_query() && is_post_type_archive(['your-custom-post-type-slug', 'your--othercustom-post-type-slug'])) {
// Set the 'posts_per_page' parameter to '-1' to show all posts
$query->set('posts_per_page', -1);
return;
}
}
add_action('pre_get_posts', 'pre_get_posts');
This code is designed to be added to your theme’s functions.php file or within a custom plugin. It hooks into the pre_get_posts action to modify the main query before it runs.
When to Use It
Use this snippet when you want to display all entries of certain custom post types on their respective archive pages without the need for pagination.
Customizing the Snippet
Replace 'your-custom-post-type-slug' and 'your--othercustom-post-type-slug' with the actual slugs of your custom post types.

Leave a Reply