Understanding the Anatomy of Secondary Loops
Next in this lesson on using secondary WordPress loops with the Thesis theme, we’ll talk about the anatomy of a secondary loop. It’s important to understand that there are actually several ways to create loops in WordPress. In fact, if you have paid attention to what I’ve taught of the last couple of years, you’re going to notice that I’ve taught more than one way myself but as WordPress has evolved, it has refined its mechanisms for doing this and documentation is not always kept up with those refinements.
If you are looking around, you’re going to find that there are descriptions of different ways for creating these things. The most common old way that you’ll see it being described is by using query_posts. Unfortunately, even in a confusing way, the codex describes using query_posts for secondary loops. If you look at wordpress.org and look at the loop, you’re going to see that there are examples of creating a main loop with query_posts and examples of creating multiple loops using the query_posts function.
WP_Query Method of Creating Secondary Loops
It’s absolutely possible to do that but it is so easy to mess up doing this that WordPress has created a system that solves most of those messing up problems. That system is called WP_Query. Remember, I first referred to query_posts as a very common method but WordPress would prefer that you use WP_Query as the method for creating secondary loops. That’s what we’re going to talk about, we’re going to talk about WP_Query.
WP_Query is the preferred method because it creates a separate query from the main query. Query_posts creates the main query so if you use query_posts to create a secondary loop, what you’re doing is, overwriting the main query. There are ways for you to store the main query and change the query for your secondary loop then restore the main query back.
There are mechanisms for doing that but it became apparent to the WordPress authors that that really wasn’t as easy a system as it could be so they created this WP_Query class for the purpose of leaving the main query alone entirely. You never have to worry about screwing up the main query and creating as many secondary queries and secondary loops as you may see fit without each one of them affecting the other. That’s really the purpose for WP_Query, is for you to create secondary loops that don’t interfere with each other and don’t interfere with the main loop.
Four Parts of Secondary Loops Using WP_Query
A secondary loop using WP_Query essentially has four parts. If we come down here and take a look at Usage, the first part is visible here. The first part is defining the query parameters or arguments and this variable here, $args is standing in for the first part where you define the query parameters.
The second part then is creating the query. It essentially says, create a new query which is a new instance of the WP_Query class with these arguments or parameters. Once you’ve defined your query, then you loop through the query results so while your new query has posts, display each individual post or get each individual posts information. That’s the third part of it, displaying the results and the fourth part is cleaning up after yourself.
That is just because we may be using template tags, we just want to clean up after ourselves and reset all those things back to their initial state so you have this Reset Post Data. Those are the four parts, defining the query parameters using these query parameters to define the query, looping through the posts found inside that query, displaying them and resetting.
What are WordPress Query Parameters
Now, really, parts 2 and 4 are very simple. Part 1 and 3 can be pretty complex and we’ll talk about the complexity of part 3 that is displaying the results as we work through our examples today. I would like to talk about the WordPress query parameters. If you look up here, you can see they’ve got their list of their categories of query parameters. They’ve got author parameters, category parameters, tag parameters, taxonomy parameters and post and page parameters.
We’re going to start off this whole thing by taking a look at category parameters and this is the list of category parameters. Now, what the category parameter does is in this case, if you use cat, it says, get all posts that are in the category with the ID of 4, cat=4 is all post of the category of 4. You could use a category name parameter which is what we’re going to be using today.
Category Name Parameters
You can use the category name parameters and what you would use instead of the ID is the slug. Now that they even said use category slug not the name, you probably know that there is a difference between the name and the slug of a category.
In fact, if we come over here and look at the demonstration site, you’ll see that fairly clearly. If we go look at my categories, the name is capitalized, Green Apples, that’s the category name and green-apples is the slug. That’s the difference between the name and the slug and inspite of the fact that the parameter calls itself category name is actually category slug.
You can also use category__and. In category__and, notice that there are two underscores, category__and, category__in, category__not_in. Category__and means that if it’s in category to and for, then do it. You can see an example of category__and down here and it has an array 2 and 6. This will extract all posts that are in both of categories 2 and 6 not all posts in category 2 and all posts in category 6 but those posts that are in both of those categories.
Category__in is very much like category name, it’s essentially giving you a list of category ids that it can be in and category__not_in is exactly the same although in the opposite. Those are your category parameters so you can select all the posts in a given set of categories.
Difference Between Tags and Categories
A Similar kind of thing happens with tags although if you look closely, you’ll notice that there are differences between tags and categories. For example, tag uses the tag slug whereas cat uses category id. In fact, when I was preparing for a class recently, I’ve just forgotten that tag was the slug and tag id uses the tag id.
Really, you end up using this WP_Query page a lot when you’re working with these because there may not be enough consistency for you not to make mistakes if you don’t come back and double check these things. In spite of the fact that it’s inconsistent in its syntax, it is consistent in what it can do that is, it can be tag slug, tag_id, tag__and means it should be in both of these tags, tag__not_in, tag_slug__and, tag_slug__in, those are all very similar.
Post and Page Parameters
We’ll skip taxonomy parameters for a moment and look at post and page parameters. If you say p=7 then it’s going to extract the post with the id of 7. The same thing is true for pages, you could say page id of 7 and it’s going to extract the page with the id of 7. You can do it by page name and you can do it with parent pages. Obviously there’s no such thing as a post parent in posts themselves but there are in pages. Pages can have child pages then post__in and post__not_in, it’s all still the same. Those are post and page parameters.
The next thing we’re going to look at is pagination parameters and the most important one here is posts_per_page. This is where you can say how many posts you want to show up in a given secondary loop. If you’ve included pagination in that secondary loop, it can create pages so if you want to display 10 posts but you’ve got 40 posts, you can use pagination in secondary loops to loop through all the posts you get to the end.
In practical standpoint, what we’re doing here is we’re using posts_per_page to limit the number of posts that are going to show up in this secondary loop. That’s enough of just plain old ordinary talking. What we’re going to do now is move in to example number one.