How to List All Posts of an Archive, Category, or a Search Result in WordPress

Home » Snippets » How to List All Posts of an Archive, Category, or a Search Result in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Navigating through a WordPress site can sometimes feel like navigating through a maze, especially when you’re trying to find specific posts in an archive, a category, or search results. Wouldn’t it be easier if you could just list all the articles under these subheadings? Well, we have a solution for you. In this article, we will guide you through the steps to easily create a list of all posts contained in an archive, a category, or a search result, simplifying content discovery for you and your audience alike.

					function wpturbo_list_all_posts() {
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
    );

    $query = new WP_Query($args);

    if($query->have_posts()) {
        while($query->have_posts()) {
            $query->the_post();
            echo '<p><a href="'.get_permalink().'">'.get_the_title().'</a></p>';
        }
    } else {
        echo 'No posts found';
    }

    wp_reset_postdata();
}
add_shortcode('wpturbo_all_posts', 'wpturbo_list_all_posts');
```
This is a simple function to list all published posts. If there are no posts, it will echo 'No posts found'. The function is attached to the shortcode [wpturbo_all_posts], so you can place that shortcode anywhere in your post where you want the list to appear.
				

The provided code snippet outlines a simple function wpturbo_list_all_posts(), which sets a custom WordPress query to gather and display a list of all published posts on your website. If there are no posts available, then the message ‘No posts found’ will be displayed.

The portion of the code that determines the type of posts queried is given by the $args variable, which is an array containing arguments to pass to the WP_Query class. post_type is set to ‘post’, which means we are querying blog posts. The ‘post_status’ is set to ‘publish’ and ‘posts_per_page’ is set to -1, which implies it fetches all posts that are published.

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => -1,
);

Next, the $query variable is assigned a new instance of WP_Query initiated with the $args array as argument.

$query = new WP_Query($args);

Following this, an if statement is implemented to check if the result of the query contains any posts. If it does (if($query->have_posts())), a while loop is initiated to loop over each post in the results.

if($query->have_posts()) {
    while($query->have_posts()) {
        $query->the_post();

Within the while loop, $query->the_post(); is used to set up the post data for each post, which allows template tags to be used within the loop. For every iteration(record), the permalink of the post is rendered as a clickable hyperlink with the title of the post.

echo '<p><a href="'.get_permalink().'">'.get_the_title().'</a></p>';

If no posts are found in the query, the function echoes ‘No posts found’. Once the loop and function are finished, wp_reset_postdata(); is called to reset the global $post variable, which restores the original query that was in place before our custom query was run.

Lastly, the function wpturbo_list_all_posts() is tied to a WordPress shortcode ‘wpturbo_all_posts’. This allows the function to be executed wherever this shortcode is added within the WordPress text editor.

add_shortcode('wpturbo_all_posts', 'wpturbo_list_all_posts');

This way, by using the shortcode [wpturbo_all_posts], all published posts can be fetched and displayed anywhere on your WordPress site.

Register an account to save your snippets or go Pro to get more features.