There are a real possible scenarios wherein you want to display all the WordPress posts on a single page. For example, you might be looking to publish an archive page or a sitemap page where you want to list all the posts without any pagination. In this article I will detail you a way using which you can list all the WordPress posts on a single page.
There are many different ways to achieve this. The most popular being the WordPress way of achieving things using plugins. I generally resort to plugins as the last resort. First I always try to achieve things using WordPress inbuilt ways, then opt for functions.php custom code way and if still cumbersome, finally I go to the plugin way and work on finalizing the plugins. Because as you know, for any task the WordPress market is fully loaded with many number of plugins. Nowadays, there is no shortage of plugins, but the real task is to identify the right WordPress plugin.
The custom page template is one of the simplest ways. It is easier to do as you might have already created custom templates. So, what is a custom page? By default, WordPress gives the option to create posts and pages and this is the standard template that comes default with the WordPress. When you want to have some custom designed template with a predefined structure, then we can create it as a template and use it for publishing an article. So the content you create will be fitted onto the template as you have designed and published.
<?php // wp-query to get all published posts without pagination $allPostsWPQuery = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1)); ?> <?php if ( $allPostsWPQuery->have_posts() ) : ?> <ul> <?php while ( $allPostsWPQuery->have_posts() ) : $allPostsWPQuery->the_post(); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul> <?php wp_reset_postdata(); ?> <?php else : ?> <p><?php _e( 'There no posts to display.' ); ?></p> <?php endif; ?>
First, we define a WP query to get all the published posts without pagination limit. Then we iterate through the results and display all the posts one by one as a list.
If this is the first time you are creating a WordPress template page, then it would be easy to use the existing template. Copy the page.php and paste it with a new filename. Replace the content part with the above script to display all the posts and save it. Change the template name to all posts. Now you go to the editor and create a new post and use this new template. Now you have created a page which lists all the WordPress posts on a single page. Tada!
This WordPress code tutorial was published on May 10, 2018.