← Back to Course

WordPress Loop & WP_Query

How WordPress Fetches and Displays Posts

The WordPress Loop is the core PHP code structure that themes use to fetch and display posts, one at a time, on any given page.

Understanding the Default Loop

A typical theme file checks have_posts(), then loops through the_post() to output each post's title, content, and metadata.

This default loop automatically uses whatever posts WordPress has already queried for the current page.

Customising Queries with WP_Query

WP_Query lets developers create a custom, secondary loop with specific parameters, such as post type, category, or number of posts.

This is commonly used to build custom sections like "Related Posts" or a featured products grid outside the main query.

ConceptPurpose
The LoopDisplays posts already queried for the current page
WP_QueryCreates a custom, targeted query for specific content
have_posts() / the_post()Functions that step through each result

Custom queries often go hand in hand with Hooks, which let you modify what WordPress does at almost any point.

Ready to master WordPress Training Course?

Join Uncodemy's hands-on, mentor-led WordPress training and build real, live websites.

Explore Course