Mastering WP_Query: Using the Loop
read more

Mastering WP_Query: Using the Loop

As I outlined in the introduction to this series, the WP_Query class has four main elements:

  • the arguments for the query, using parameters which will be covered in detail in this series
  • the query itself
  • the loop, which will output post content, titles or whatever you want to display
  • finishing off: closing if and while tags and resetting post data

In this tutorial I’ll show you how to use the loop with WP_Query, including the two main ways to structure your loop and how to use multiple loops.Where the Loop FitsWithout a loop, nothing will be displayed on your page. After WordPress has run the query, using the arguments you’ve defined, it then needs to be told what to output from the data that it’s fetched. This is where the loop comes in.