Need help understanding The Loop

<?php
while(have_posts()) {
the_post();?>
<h2><a href=”<?php the_permalink()?> “><?php the_title();?></a></h2>
<?php the_content(); ?>
<hr>
<?php }
?>

What do have have\_posts() and the\_post() actually do? I get that have\_posts returns a boolean, but is the context different depending on whether The Loop is in an index, single, or page file? So for index I guess it finds the blog posts in the database, but for page the\_post will just select all the content the user inputted in the admin panel?

the\_post is essentially iterating over all the posts in database while at the same time selecting the most recent one? Is that how it works?

1 Comment
  1. [https://developer.wordpress.org/reference/functions/have_posts/])

    It works off of the global $wp_query, which is, as the name implies, an instance of wp_query:

    [https://developer.wordpress.org/reference/classes/wp_query/])

    The loop in general is explained a bit here:

    [https://developer.wordpress.org/themes/basics/the-loop/])

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer