WordPress Show All Posts on a Custom Post Type Archive Page
WordPress archive pages show a limited number of items, controlled from the Settings > Reading > Blog pages show at most
section. The default is 10, and it applies to all archives, including Custom Post Types.
If you want to leave your posts page at 10 but show all of your portfolio pieces, what can you do? Well you could code up a new template and create your own fresh query. But you’d be doing it WRONG!
pre_get_posts
action to the rescue!
pre_get_posts
is a filter that allows us to modify an existing WP_Query
, before that query is actually run. This means we can filter for the number of posts to show.
Here’s a gist to show all posts in the Portfolio CPT archive page. Place this code in your functions.php
file, or your functionality plugin:
/**
* Show all Portfolio CPT items on archive
*
*/
add_action( 'pre_get_posts', 'spigot_show_all_work' );
function spigot_show_all_work( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( is_post_type_archive( 'portfolio' ) ) {
$query->set('posts_per_page', -1 );
}
}
}
This is how we do it here on Spigot. Notice that our work page shows all the work we’ve published, without the annoyance of pagination!
Is there a way of filtering out one of the authors from this scheme? Let’s say a user with the ID=’4′ doesn’t want to have an author archive with portfolios, but only with plain posts. Is it possible?
Thank you.
Hi Dan,
Not quite following you here. Are you looking to filter a user out of the portfolio archive, or simply show ALL posts by an author?
You can also use
pre_get_posts
to show all posts on an author archive page. Try this in the query:if ( $query->is_author() ) {...}
Hi!
We’re facing the same problem now. The issue is that WordPress in spite of using $query->set(‘posts_per_page’, -1 ); or set_query_var(‘posts_per_archive_page’, -1); still process archive pages!
For example, you need to make single archive page for all the posts you have there:
example.com/movies
and you set posts_per_page -1 in your functions.php, but wordpress will still process and return pages:
example.com/movies/page/2, example.com/movies/page/3 etc.
Now, I’m trying to find out the solution.
If you’ll got some – please write a reply here, I’ll check notify.
Hi Rubika. By setting
posts_per_page
to-1
you are telling WordPress to show all posts.If you are trying to prevent WordPress from creating an archive for a custom post type, you’ll need to set
has_archive
tofalse
.I hope that helps.
No, the main idea is to keep archive page but prevent existing pagination pages that WordPress keep processing with direct access. For example:
example.com/portfolio/page/2
Solved with detecting ‘page’ variable and send 404 headers
To show all the CPT s elementor wants your to use one of their archive widgets. This gives you options on how to display the archive, however what this widget does is show a collection of mini previews to the CPT s like what you see when you create a blog.
Hi RunPower. If you’re using Elementor to show page archives, the snippet above will be of little use to you.