WordPress loops

Standard WordPress loops

The WordPress loop is preceded by some query to the database. A “Standard query” is a query that collects data about standard posts, from which it can be concluded that a “standard loop” is a loop that “spins” the result string of a standard query. The results of a standard query from the MySQL database are stored in the form of a $wp_query object:

WordPress loop structure

At the heart of the WordPress loop is the while loop:

A while loop in WordPress looks like this:

The WordPress loop consists of three parts:

  1. $wp_query->have_posts()

    $wp_query->have_posts() points to the method of the $wp_query object and when the standard query is to the base, then the abbreviation have_posts() is used in the loop. This function checks whether the current WP query to the database has any result, i.e. whether the $wp_query object has a value. It’s a boolean function so as long as it’s TRUE, the loop runs.

  2. $wp_query->the_post()

    $wp_query->the_post() is also a method of the $wp_query object and here the shortcut the_post() is used for the standard query. This method fetches the next post, and makes available inside the loop the global variable $post and whatever that global variable provides.

    This part of the loop doesn’t print anything, it just tells the next part of the loop (which is some template tag) in which current circle the loop is located, and in which template it is in the loop:

    • $post>ID – ID of the current post.
    • $post–>post_category – Retrieves the ID of the post category.
    • $post–>post_parent – ID of the parent page. Useful for creating custom navigational elements.
    • $post–>post_title – Post Title
    • $post–>post_excerpt – Post excerpt
    • $post>post_content – Retrieves all of the post content, along with any markup.
    • $post–>post_name – Retrieving the slug of a post.
    • $post–>guid – Post Url
    • $post–>post_author – ID of post author post_parent
    • $post–>post_type – Returns the type, page or post.
    • $post–>menu_order – Returns the menu order as set in the post/page editing window. Often menu items are sorted via this value.
    • $post–>post_date – Retrieves the integer timestamp for when the post was published. The output can be customized.
    • $post–>post_modified – Retrieves the integer timestamp for when the post was last modified.
    • $post–>post_status – Retrieves one of five possible posts statuses: publish, private, draft, pending, future.
    • $post–>comment_count – Returns the number of comments, pings, and trackbacks for a given post.
  3. Template tag

    Template Tags are used in the central part of the loop because they take data from the database. This data can be anything related to the post, post title, post content… Through some template tags, you canpassed and parameters, which allows different data from the database to be collected with the same tag and different parameters. There are a number of template tags that can be used such as:

    • the_content() – prints the complete content (only available in a loop)
    • the_excerpt() -prints a shortened version of the content (only available in a loop)
    • next_post() (only available in a loop)
    • previous_post() (only available in a loop)
    • the_title() – prints the title of the post/page
    • the_ID() – prints the ID of the post/page
    • the_permalink() – prints the link to the post/page
    • the_author() – prints the name of the author who published the post/page
    • the_category() – prints the category of the post under which it is filed (or more)
    • . . .

A standard WordPress loop consists of an IF loop that runs a while loop only if there are posts. An example of a standard WordPress loop:

But it is common for WordPress to display a loop like this:

or even “prettier” like this:

Link between loop and page template

It should be noted that loops that look exactly the same can display multiple posts on a page or just one post. This is made possible by wordpress looping after it “recognizes” which template it is in, it “knows” exactly what it should display, a list of posts or just one post. Therefore, it will show different results if it is in index.php or single.php.

Example

The following wordpress loop written within the index.php template will print all written posts:

While the same loop but located in the single.php template will print only one post:

Practically, the same loops but display different data because they are in different templates.

Custom WordPress loop

Custom wordpress loop is used with an object that contains a specific query to the database and is created from the WP_Query() class. The custom loop is similar to the standard loop, except that the standard object $wp_query is not used, but a custom object $spec_upit. For this reason, abbreviations are not used for the methods as in the standard loop, but the full nomenclature:

  • $spec_query->have_post
  • $spec_input->the_post()

In this loop, the method of the object $spec_upit->the_post() makes available the global variable $post just like in the standard loop, except that this global variable now contains data about the custom query to the database and stores that data in the object $spec_upit. This means that different data will be available in the loop through the global variable $post than in the standard loop.

That is why it is necessary to return the standard global variable after the loop is finished. In order to restore the standard $post variable, it is necessary to call the function wp_reset_postdata() at the place of the code where the loop ends, i.e. immediately after endwhile.

Example:

Reset WordPress loop

It already ismentioned that with every use of the custom loop it is necessary to reset the loop, i.e. to return the standard global variable at the end of the loop. This is also necessary in the case when we have several consecutive loops in one template. There are several ways to reset the loop and which one we apply depends on the case by case.

wp_reset_postdata()

This function is applied whenever a custom query is used, but also with multiple queries. The function returns the global variable $post to the current post in the main query.

rewind_posts()

This function is used if we want to use the same loop in two places in the same page template. As its name suggests, it rewinds the loop to the initial post. While the previous functions reset the entire $wp_query object, this one does not, but instead uses the same query that “rewinds” to the beginning.