Chapter 8. Dissecting the Loop and WP_Query

IN THIS CHAPTER

  • Defining the Loop

  • Wrangling the Loop with plugins

  • Developing custom and multiple Loops

  • Using Loops strategically

If you've made it this far in this book, you understand how to build plugins, use the WordPress hook system to alter the way WordPress behaves, develop themes to present the content to the user, and leverage the widget system to modify the look and feel of a blog. In this chapter, I address the single most important piece of WordPress: the Loop.

The Loop is what makes a blog a blog. The Loop uses something called a query to grab the data from the database, organize the posts and comments, and display them. By default, the Loop is displayed in a reverse chronological order, with the most recent post appearing first on the page. However, bloggers have become very creative with how they display their content and WordPress provides some assistance.

Note

The Loop and the query are similar, yet two different things. The query retrieves data from the database according to parameters passed (or assumed based on default). The Loop iterates over this data and displays it on a blog. The Loop and the query are often referred to interchangeably but, in practice, they do two different things in conjunction with each other. One could say that they represent the perfect WordPress marriage.

Upon the Loop initialization, WordPress looks for "sticky" posts. Sticky posts are the posts that the user has requested be "stuck" at the top and ...

Get WordPress® Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.