This is Part 3 in a series of posts that try to answer the question:
What are some good resources for someone who wanted to learn more about WordPress theme development?
I encourage you to checkout Part 1 & Part 2 in this series if you have not yet: Theme Developer’s Toolkit: Part 1 and Theme Developer’s Toolkit: Part 2.
In Part 1, we learned how a WordPress theme combines a few template files together to create a single page in WordPress. In Part 2, we learned about utilizing the WordPress Template Hierarchy to communicate with WordPress about which template we want to use for specific pages. Now we are going to discuss the WordPress Loop. More specifically, we will look at a tool that every WordPress Theme Developer will find super handy: Generate WP’s WP_Query Generator.
What is a database query?
Consider a blog that has 100 posts. Each of those posts is stored in the site’s database. Scary?! No, not so scary.
When WordPress wants to display a feed of blog posts, it will go to the database and perform a query. In this case it will say, “Hey, database may I please have all the posts.”
When WordPress wants to display all blog posts in the category Tutorials, it will go to the database and perform a query. In this case it will say, “Hey, database may I please have all the posts in the category Tutorials”.
So, a database query is simple a question of the database to retrieve content of a specific type. In a WordPress template we often perform queries in order to display content. In the first example, where we want to see all blog posts, we would likely be forming a query like this for the blog template. In the second example, where we want to display only posts from a specific category, we would likely be forming this query on a category archive or perhaps if we wanted to feature a specific blog category on our home page.
What is the WordPress Loop?
The WordPress Loop is what you do after the query has gathered up the content we are looking for. Let’s take our first example of a blog website that has 100 posts. We want to display all those posts on our blog page.
Rather than code that we want to display the title of blog post 1, the featured image of blog post 1 and an excerpt from blog post 1… and then do the same for blog posts 2 – 100, we use what is called the WordPress Loop.
The WordPress Loop allows us to say while we have these 100 blog posts, go through them one at a time and perform these actions for each post. In this case the “actions” we want performed are to display the title, the featured image and an excerpt.
Now that we understand the concept behind a query and the WordPress Loop, let’s take a quick peek at the tool we are featuring in this installment of the WordPress Theme Developer’s Toolkit.
Tool: WP_Query Generator by GenerateWP
GenerateWP offers a tool on their website called the WP_Query Generator. When we code a query for WordPress to ask the database we have to format it a special way and make sure we are communicating correctly the type of information we want returned. The WP_Query Generator tool allows you to fill in fields that describe the information you are looking for, in return it provides a formatted query that you can use in your WordPress templates.
Whether you use this tool to creates your queries for you or you simply play around with some values to see what is possible with a database query — I think it is a useful tool in working towards understanding WordPress Theme Development.
The code found below was generated by WP_Query Generator after I went to the “Type & Status” tab and entered in “post” under the label Post Type. I also went to the pagination tab and entered in the value “-1” under the label Posts Per Page (-1 means return all posts).
The code below shows how we setup our array of query arguments, which we are setting equal to a variable called $args. We then perform our query by using the WordPress function WP_Query(). Lastly, we have asked the WP_Query Generator to include a loop which we would use to perform the same action on each individual post (show it’s title, etc.).
// WP_Query arguments
$args = array(
'post_type' => array( 'post' ),
'posts_per_page' => '-1',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// show the title, the featured image and the excerpt
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();