WordPress Loop and Query: A Comprehensive Guide

WordPress Loop And Query

In this article, we’ll see about WordPress Loop And Query.

WordPress Loop And Query

The WordPress Loop and Query are two of the most important concepts in WordPress development.

The Loop is responsible for displaying posts on your website, while the Query is responsible for retrieving those posts from the database.

We will take a look at the basics of the WordPress Loop and Query.

We will discuss how WordPress Loop and Query work, how to use WordPress Loop and Query, and how to customize WordPress Loop and Query to your needs.

The WordPress Loop

The WordPress Loop is a PHP code that is used to display posts on your website. The Loop works by iterating through the posts in the database and displaying them one at a time.

The Loop is made up of two main parts: the while loop and the the_post function.

The while loop is used to iterate through the posts in the database. The the_post function is used to display the current post.

The following code shows an example of the WordPress Loop:

while ( have_posts() ) {
the_post();

// Display the post content
the_content();
}

The have_posts() function checks to see if there are any more posts to display.

If there are, the while loop will continue iterating. If there are no more posts to display, the while loop will terminate.

The the_post function retrieves the current post from the database and sets up the global $post variable.

The global $post variable contains all of the information about the current post, such as the title, the content, and the author.

The WordPress loop is PHP code that displays WordPress posts. The loop is used in WordPress themes to display a list of posts on a web page.

Inside the loop, there are some functions that are run by default to display posts. Theme developers can format the output by using template tags to customize how each post inside the loop is displayed.

There are several Template tags that work only inside the WordPress loop and can be used to format, arrange, and publish post data.

The WordPress loop is arguably one of the most important aspects of the WordPress code and at the core of most queries in one way or another.

It is one of important concept from WordPress Loop and Query


The WordPress Query

The WordPress Query is a PHP function that is used to retrieve posts from the database.

The Query can be used to retrieve posts based on a variety of criteria, such as post type, post status, and date.

The following code shows an example of the WordPress Query:

$args = [
‘post_type’ => ‘post’,
‘posts_per_page’ => 5,
];

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();

// Display the post content
the_content();
}
} else {
echo ‘No posts found’;
}

The WP_Query constructor takes an array of arguments as its input. The arguments control how the Query will retrieve posts from the database.

The posts_per_page argument controls how many posts will be retrieved per page. The default value for this argument is 10.

The have_posts() and the_post functions work in the same way as they do in the WordPress Loop.

Loops go hand in hand with queries. Loops allow you to display posts by iterating through a list of them, but how does it know which ones to show you from all the hundreds in your database? The answer is The Query.

Similar to loops, there is the main query which can be called The Query (capitalized) and there may be other custom queries on the page.

The 10 recent posts are pulled using the main query while the random posts are retrieved using a custom query. The main query is always generated by WordPress.

WordPress looks at the URL you are on, and figures out what type of page you are looking at and what posts belong there.

Let’s assume you visit the “Design” category on WDD, the URL is http://www.webdesignerdepot.com/category/design/.

It is one of important concept from WordPress Loop and Query

WordPress figures out that you are on a category archive, namely the design archive. Instead of retrieving your latest 10 posts, it looks for the latest 10 posts within this category.

This information can then be used by the theme to create a loop to display the posts with. As a result, each page you are on will use a different main query.

Even if the code of the loop is the same, you will see different content since the query retrieves different posts.
An example loop

Below is an example of The Loop. It can be used on any page such as your home page, archive pages, search pages, etc.

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post() ?>
// Post display here
<?php endwhile ?>
<?php else : ?>
// Content if there are no posts to show
<?php endif ?>

If there are no posts, we show a sad message informing users that there are no posts here.

If there are posts, however, we loop through them one by one and display them. Here’s a complete block of code that will actually display posts.

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post() ?>
<div <?php post_class() ?>>
<h2><a href='<?php the_permalink() ?>'><?php the_title() ?></a></h2>
<div class='post-excerpt'>
<?php the_excerpt() ?>
</div>
<div class='post-meta'>
<time><?php the_time( 'Y-m-d' ) ?></time>
<?php if ( has_category() ) : ?>
<span class='post-categories'><?php the_category( ', ' ) ?></span>
<?php endif ?>
</div>
</div>
<?php endwhile ?>
<?php else : ?>
<h2>Sorry, there are no posts here</h2>
<p>Perhaps you would like to go back to the <a href='<?php echo site_url() ?>'>home page</a>?</p>
<?php endif ?>

A quick reminder: the content is determined by the query. How the content is shown is determined by the loop.

This is what gives WordPress (and other server-side language-based systems) such awesome templating power.

You can use the very same code above to display any post list.

On the author page, the most recent articles from the author will be in the query. On a search page, the most recent results matching the query are retrieved.

The loop just goes through the items WordPress retrieves for us.


Modifying the Query

While it is possible to modify the query, it is not recommended for various reasons which I will get into later.

That being said, it is a good step on the path to learning more about loops and queries so let’s take a look.

Let’s say you want to exclude a particular category from your home page. If you know the ID of this category you can do so by placing the following code in your index.php, before the loop.

<?php
if ( is_home() ) {
query_posts( 'cat=-92' );
}
?>

You can use a fair number of parameters that would allow you to modify how many posts are shown, include/exclude tags, categories, show posts from a particular set of authors, and so on.

<?php
if ( is_home() ) {
query_posts( 'cat=92&author=4,11,392,2' );
}
?>

The code above shows only those posts that belong to category 92 (this may be a featured category) and were written by one of four authors, perhaps your top-ranking ones.

So why is this not recommended, despite being quite easy to pull off? The main reason is that it is inefficient and it breaks paging (unless you fix it by adding the paged parameter).

While it seems like you are modifying the original “The Query”, that is not what’s going on.

The original query runs just the same, pulling your latest 10 posts.

WordPress then sees your query_posts() function and re-runs the query with your category constraint in place, rendering the initial query wasteful.

Creating our own queries

Creating our own queries won’t solve the problem above, but it’s the next step in learning to do so.

Creating queries is useful when you need something extremely specific, or if you want a secondary loop, like our random 3 posts example. A custom query with a custom loop looks something like this:

<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'rand',
'posts_per_page' => 15
);
$random_posts = new WP_Query( $args );
if( $random_posts->have_posts() ) :
while( $random_posts->have_posts() ) :
?>
Post Display Here
<?php else : ?>
You have no posts to show
<?php endif ?>

The first bit down until the if statement defines the query.

Using the $args array I specified that I’d like to retrieve 15 random posts which have the “post” post type (regular blog posts) and have been published.

I then used this array to create a new query object.

If you don’t know about object-oriented PHP just yet, no sweat, just remember the notation!

The rest is a slightly modified version of our initial loop. Instead of using have_posts() and the_post() on their own, we prefix it with the variable we used to store our query in $random_posts.

There are lots and lots of parameters you can use as arguments.

You can pull posts from specific dates or date ranges, you can grab posts based on complex category/tag/taxonomy rules, and you can even use data in the metadata table to narrow down the list.

Take a look at the WP_Query Class Reference for more information.

Creating our own query is great, but it still doesn’t solve our original problem.

If we wanted to exclude categories on the home page we could build our own query to do so, but this wouldn’t prevent the main query from being run.

The final piece of the puzzle is the pre_get_posts hook which allows us to modify the main query.

Modifying the main query

A note of caution before we continue: modifying the main query can have serious unintended consequences if not done correctly.

Experimenting is safe as long as you do it in a well-defined location so you can remove your changes if you see something odd.

In WordPress, hooks are a mechanism that allows us to modify core functionality. They enable us to change the excerpt length, modify the login screen, re-phrase error messages, create custom post types, and more.

By using the pre_get_posts hook, we can modify the parameters of the main query before it is performed. You’ll need to add the following to your theme’s function file, or even better: into the functions file of a child theme.

add_action( 'pre_get_posts', 'my_exclude_category' );
function my_exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-92' );
}
}

This bit of code achieves what we were trying to do previously — exclude category 92 from our home page — but without the overhead. The main query itself is modified. So, why is this dangerous? Did you notice the

is_home()

and

is_main_query()

functions? These make sure that the query is only modified on the home page and if it is the main query.

Let’s look at an example without the proper safeguards.

Perhaps you noticed that an author may have plagiarized and you want to remove all traces of his/her posts until the issue is resolved. You do something like this:

add_action( 'pre_get_posts', 'my_exclude_author' );
function my_exclude_author( $query ) {
$query->set( 'author', '-23' );
}

This does actually exclude the author’s posts from everywhere, but you’ll also be astonished to see that they disappear from the back end as well. This is why conditional functions are so important.

Take a look at all of them in the WordPress Codex. Here’s what this code should look like in the end:

add_action( 'pre_get_posts', 'my_exclude_author' );
function my_exclude_author( $query ) {
if( !is_admin() ) {
$query->set( 'author', '-23' );
}
}

The WordPress Loop and Query are two of the most important concepts in WordPress development.

The Loop is responsible for displaying posts on your website, while the Query is responsible for retrieving those posts from the database.

In this blog post, we took a look at the basics of the WordPress Loop and Query.

We discussed how they work, how to use WordPress Loop and Query, and how to customize WordPress Loop and Query to your needs.

Hope this article helps to learn WordPress Loop and Query!

Write a Reply or Comment

Your email address will not be published. Required fields are marked *