How the WordPress Loop works in the WordPress theme

The WordPress loop is an important part of every WordPress theme and is used to display all posts. This tutorial explains the application and functionality of the WordPress loop in detail and shows how and where the loop is used.

What is the WordPress Loop?

The WordPress Loop is used to display posts in the WordPress theme. It is therefore a central component of every WordPress website. As the name Loop suggests, it is a loop in the program code.

The WordPress loop is often run through several times, for example to display several posts on the blog page and in archives. The loop is also used for individual posts and static pages; the loop is only ended after one pass.

The WordPress loop is technically implemented with some PHP code as a while loop :

 while ( have_posts() ) : the_post(); // Display Post Content get_template_part( 'template-parts/content' ); endwhile;

Example: To display 10 posts on the blog page, the while loop in the theme is run 10 times. Each individual post is represented with the code within the loop. Usually an additional template file is loaded for this purpose.

Where can you find the WordPress Loop?

The loop in WordPress is closely linked to a database query. To display content in WordPress, a query with WP_Query is almost always carried out first and the result is then output in the WordPress loop.

This link becomes apparent when looking at a custom query and custom loop:

 // The Query $custom_query = new WP_Query( $args ); // The Loop if ( $custom_query->have_posts() ) { while ( $custom_query->have_posts() ) { $custom_query->the_post(); // Display Post } wp_reset_postdata(); }

I'll be writing another article soon on the subject of custom queries.

Most of the time, the WordPress Loop is the default loop that can be found in the template files of the theme. This standard WordPress loop in the theme is used to display the WordPress main query .

The WordPress loop in the template files of the theme is also linked to a database query, even if this cannot be found here directly in the code of the theme, but has already been executed by the WordPress core.

What does the WordPress loop look like in the theme?

Not all template files in the theme have the task of displaying the WordPress Main Query. Typical templates that have a WordPress loop are, for example, index.php, home.php, archive.php, single.php and page.php .

Other files such as header.php, footer.php, comments.php or sidebar.php are only components and therefore do not contain a WordPress loop. You can find more details in my post about the different template files in a WordPress theme .

The WordPress Loop could for example be used in the archive.php :

 <?php if ( have_posts() ) : the_archive_title( '<h1 class="page-title">', '</h1>' ); // Start the loop. while ( have_posts() ) : the_post(); get_template_part( 'template-parts/content' ); endwhile; the_posts_pagination(); else : get_template_part( 'template-parts/content', 'none' ); endif; ?>

At the beginning it is checked whether there are any contributions. If not, a corresponding message is output with content-none.php . Otherwise the title of the archive (category, keyword, month archive) is displayed and the WordPress loop is started.

The while loop (= WordPress Loop) is repeated until all posts are completely displayed. By default, archives display 10 posts per page and numbering older posts is offered. The_posts_pagination () does this after the end of the WordPress loop.

Individual contributions are shown in the single.php .

A WordPress loop is also used here:

 <?php // Start the loop. while ( have_posts() ) : the_post(); get_template_part( 'template-parts/content', 'single' ); comments_template(); endwhile; ?>

The single.php is only called if the contribution exists. Otherwise WordPress will show the 404.php. Therefore an if-query can be dispensed with. Instead of content.php , content-single.php is loaded here to display the post. The comments are also included.

The functions of the WordPress loop

Now let's take a closer look at the functions in the WordPress Loop:

have_posts ()

A look at the function shows that it only consists of two lines:

 function have_posts() { global $wp_query; return $wp_query->have_posts(); }

It is a so-called wrapper function. WordPress saves the main query in the global variable $ wp_query , which is an object instance of the WP_Query class. The wrapper function makes this variable accessible and calls the have_posts () method of WP_Query .

WP_Query :: have_posts () checks whether there are more posts in the WordPress Main Query that can be displayed. The method returns true until the internal counter of WP_Query has reached the end. The return value is false for the last post or if no posts at all are found.

Due to this fact have_posts () can be used in two places:

1. To check whether there are any posts at all
if ( have_posts() )

2. To run through all posts in the while loop as a while condition
while( have_posts() )

the_post ()

The_post () is also just a wrapper:

 function the_post() { global $wp_query; $wp_query->the_post(); }

The WP_Query method the_post () is used to set up the post and calls two additional methods for this. On the one hand the function next_post () , which counts up the internal counter $ current_post by 1.

On the other hand, the method setup_postdata () , which sets some global variables for the post and thus makes various template tags possible within the loop. Without the call to the_post () , many template tags within the loop would not work as desired.

Example: The global variable $ authordata is defined in the post setup, which is used by the template function the_author () to display the author.