We’ve created a function to Add a List of Posts From a Single Category and we’ve hooked that function to a specific place in Genesis. That’s what we do with all functions, whatever it is we’re trying to do inside of a function, and we hook it into a specific location. So we’ve done that.
Different Ways to Create a Secondary Loop
Now we need to create this query, this secondary loop. There are a couple of different ways to create a secondary loop.
Query String Style
The first way is something that you see reasonably often in examples, although not in any of these examples. But as soon as you get down to the examples of their query variables, you can see that they use a syntax like this ‘author=123’ or ‘author=2,6,17,13,38’ or ‘author__in’ array 2,6. This is called a query string style of defining a query.
The Associative Array Method
The other way is the way that I’ve used here and that we’re going to use consistently. That is the associative array method. So rather than ‘cat’ => 2 we’re saying here’s our array. It’s an associative array and that associative array contains a set of named value pairs.
Using The Associative Array Method
So the name of the first element in the array is ‘cat’ and the value of of that is 2. And now we’re going to start working on complex queries that have lots of parameters.
You could say ‘cat’=2 also works. This is called the query string annotation or the query string method and the problem with it is that if you have multiple things it becomes quite difficult to read. So we are not going to use this method.
Why We’re Using It
We’re going to use a method that’s easy to read which is the second method. And, as I said, it’s this associate of array where we define the property of the first element in the array and then we give that property a value. Then that becomes a variable because this thing called $args equals this.
For some of you variables are brand new concept and for those of you for whom variables are a brand new concept, you need to know that variables always begin with a dollar sign ($) in PHP.
This is a variable called $args and we are saying that it is equal to an array of some content, in this case, a content of ‘cat’ with a value of 2. Once we’ve defined the variable, we’ve placed that variable here as the arguments for WP_Query.
Parameters of a WordPress Query
This seems kind of innocuous but what the majority of the documentation here is about are about the parameters of the query. So you have Author Parameters, Category Parameters, Tag Parameters, Taxonomy Parameters, Search Parameters, Post and Page Parameters, Password Parameters, Type Parameters, Pagination Parameters, Order By Parameters.
Order By Example
Let’s look at Order By for example. This has just changed in the latest version of WordPress because Order By has become quite a bit more interesting now than it used to be. Order By has essentially two pieces. It’s Order By and then you can choose what you want to order it by, like alphabetically by title and then the order whether it’s ascending or descending.
You can do it alphabetically by name which in this case, is the post slug rather than the post title. You can order it by date or the date it was modified or the author name, or the id. You can order it by random. There are all kinds of ways in which you can order these things but these are order by title, order descending and these are query parameters.
Full Reference to Query Parameters
Your full reference to query parameters exists right here in the wordpress.org codex under Class Reference/WP Query.
Display Posts By Specific Category
So if you want to find out how to display all the posts of a specific category then you can go to the Category Parameters. It has essentially these 5 elements, Category or ‘cat’. And ‘cat’ can either take a single number or it can take an array of numbers but those numbers are the category id.
You can use ‘category_name’ which uses the slug, not really the nice name of the category. There is ‘category__in’ which uses an array of category ids and ‘category__not_in’ which also uses an array of category ids, ‘category_name’ can be an array of slugs.
Here’s an example of that ‘category_name=staff,news’. It can be multiple categories.
If you are using ‘category_name’ then this is going to display all of the posts that have either staff or news categories and that’s what the comma in between them is for. But if you use the plus (+) sign in between them then you’re saying all those posts that have both category staff and news.
Difference Between ‘cat’ and ‘category_in’
The difference between ‘cat’ and ‘category__in is that ‘cat’ takes all of the posts and all of their children whereas ‘category__in excludes the children. In this case for example, if I were to use the ‘category__in parameter here, nothing would display because all of my posts are actually in child categories – Yellow Apples, Red Apples, Green Apples.
Let’s change this to ‘category__in. It’s is a double underscore for that. So, if I use ‘category__in for this instead of ‘cat’, nothing is going to show because I don’t have anything in the Apples category, everything is in child categories of Apples. That’s the distinction.
Use the Concepts and Apply them to Other Properties
We’re not going to talk about all of the different query variables because what really matters here is the concept not each specific possible property.
These kinds of distinctions exist in most of them so you can generally assume that you will be able to select posts that either have a specific taxonomy or don’t have a specific taxonomy, either parents only or are parents and children. You will be able to get that kind of data back, you can see the tags are very similar to categories.
Then we get down to taxonomies and it’s a little bit more interesting there but we won’t be working on taxonomy queries tonight but in any case, there’s a lot of different potential query variables that you can use here.
Main Loop, Secondary Loop and the Correct Query
Let’s go back to ‘cat’ in this function. So we’ve got our query variables. Then we have the most important statement in this whole process which is new, WP_Query. What you’re doing rather than using the existing query, the query that is already running in WordPress, is you’re saying, “Let’s create a brand new query. We’re going to create it, display stuff and throw the query away after it’s done displaying”.
The main query gets saved in memory all the way through to the end of the rendering of the page whereas the secondary queries you can save their information only as long as it takes to display that one bit of data and then you can throw the query away.
Why Use new WP_Query vs wp_query
This is why you want to make a distinction between the main query and these secondary queries and it’s why you want to use WP_Query like this rather than what thousands of tutorials out there use, the function called wp_query.
If you’re an advanced developer then you know what you’re doing and using wp_query is no problem. There are many instances where a theme author might in fact, want to use wp_query but that’s generally for things like pagination and it’s not really the sort of thing that we’re going to talk about here.
I just want you to note that most of the thousands of tutorials that talk about creating secondary loops talk about using this wp_query function. That’s a very bad practice because wp_query is always the main query. When people realize you’re having problems with it then you’d take wp_query and you’d save it in some variable and then you go mess up the main query with your secondary query.
new WP_Query Doesn’t Affect the Main Query/Loop
Then when the secondary query was done you’d go return the variable that you saved back into the main query and it was just a bunch of manipulation that gave people a lot of fits and made lots of opportunity for error whereas this using WP_Query doesn’t affect the main query at all so you never have to worry about that being a problem.
Review our Function
So we’ve got our arguments, we’ve got our definition of the query and then we’ve got what people generally consider to be the output loop. So we’ve defined what we want to return, we’ve gone and gotten the post with this. Then we’ve said if this query have_posts then do such and such and if the query has posts while the query has posts, the query the_post, this is essentially saying for each individual post do the stuff below the query the_post.
In this case, what we’re saying is, for each post echo an ‘li’ element. An opening list item tag, get_the_title which is the title of the_post here and then give us a closing ‘/li’ tag. So we started this if this query has any post at all, so if there are any post in category 2 then write a ‘ul’ tag or opening unordered list tag. Then for each individual post create a list item and insert the title and then we have this if have_posts we also have the potential for an else.
Now, as a practical matter we don’t really need an else here. You could put an error message in or something like that but really, you don’t want to display anything if there aren’t any posts, so we’re just going to go ahead and delete that. Then we just want to make sure that any remaining query memory here associated with this query goes away. So then we call the function wp_reset_postdata.
4 Basic Elements of a Query
This has essentially 4 pieces. You define the query, you use the query to go get the posts, you loop through those posts and do a certain kind of output and when looping through those posts is finished then you just go throw the memory of the query away. Those are the 4 basic elements of a query and the 4 basic elements of a secondary query.
It’s the definition of the query, it’s the actual query going and getting stuff, it’s looping through all the posts returned by that query and for each post outputting something and then it’s throwing the memory away.
WordPress Function get_the_title and the_title
Now, while we’re here we may as well look at this WordPress function get_the_title. Each of these things are about returning some aspect of the post. There are two functions that are sort of like this, there’s the_title and there’s get_the_title.
the_title actually echoes the title and get_the_title just returns the information about the title so it does not print the title, it just brings that information in some place. So you could give it a variable, the variable name equals get_the_title and it’ll put the title in that variable or you can say here as we have, echo get_the_title and then it will print the title out.
It can take one parameter and that is the id of the post in particular but if you leave that parameter out then it will get the title of the post that’s active in the loop that it’s inside of. So we don’t need to put a post id in here because it will get that from its active post inside of this loop.
Now, we’ve got echo get_the_title. We could instead have used it’s corollary. In fact, we can just do it below there for fun, the_title. This prints the title out without an echo statement. Of course, it doesn’t print the opening and closing list items so if you’re going to do it in the middle of a statement like this, it’s probably best to just do it like this using get_the_title rather echoing the ‘li’. And then the_title and then echoing the closing ‘/li’.
But that’s the difference between get_the_title and the_title. get_the_title just gets the information, the_title actually prints out the information.
There are lots of examples of these kinds of companion functions in WordPress but in this case, we’re getting the title and that title is the title of the post.