This is the 44th in a series of live lessons on using WordPress and Thesis as a Content Management System (CMS). In this lesson we modify the custom loop for our custom catalog page. This custom loop will display recent posts organized by custom taxonomy. We use the new tax_query parameter to limit the display of posts to a single taxonomy term. This video gives an extended discussion of the new WordPress tax_query parameter.
[This post contains video, click to play]
Video Transcript
Okay so now what we have is we have a bunch of teasers. And if you’re paying attention as I scroll through here, you see… Madame Blueberry’s being repeated and it’s being repeated again. And the reason for that is that we discriminated about which post is supposed to show except that we said we want to show catalog items. And so what it’s done is every time… for each of the location names, it showed catalog items and it showed essentially, the 1st 10 catalog items over and over and over again which doesn’t do any good, right? That’s not what we want to accomplish and so, this is where we have to add to our array. Because now what we have to do is we have to add the new taxonomy query. Now this taxonomy query is something that has been added to 3.1 and what it does… WordPress 3.1 and what it does is it allows us to string a group of taxonomies together in a query. So theoretically, we could say, “Show me all of the books under fiction that have been… that are in a specific location, that are for children, that have a cal number of such and such.” And the taxonomy query allows us to string those various taxonomy parameters together into a single query. And before 3.1, that was impossible.
Well so what that means then though is that a taxonomy query is not just a name value pair. It’s actually an array of arrays. So it’s a… and each taxonomy that’s in the query is one of these arrays. Now in that regard, we… well, let’s just go ahead and get started with that.
So we’ve got our args equals array, now we’re going to add another argument to that which is tax_query and then again, array and we’ll drop down again. And that’s our first array, here’s our 2nd array and our 2nd array is for the one taxonomy we want to use and so that’s the first field is the name of the taxonomy. Taxonomy…I’m messing that up…taxonomy and its location…so that’s a taxonomy we want to query. The 2nd thing we want to do is we want to pull out that… the name of the taxonomy as the thing we care about so we are going to pick the field and that is the field name in this case and that field name is going to be the slug, that’s what we want to look for. So we’re going to look at the slug and then in the slug… it’s not a semi-colon, that’s a comma. And in the field name slug, in the taxonomy name location, we want to find the term of this location name that we got right up here, location name there. So we’re going to copy that variable, put it here and call it good.
So what’s happening now is inside of the post type catalog items with the taxonomy of location who has… whose slug has the location name, show all those posts. That’s what’s going on here.
Now someone has joined us here and don’t worry about the conversation. I mean, this is a very advanced conversation we’re having right now. When I get around to indexing this, this is going to be indexed not as an intermediate tutorial. This is going to be an advanced tutorial because this is advanced programming that we’re doing here in respect to WordPress but…so there’s a lot to learn before you get to this point. And having said that, next month, we are going to start having a 3rd session and in that session, we’re only going to take beginner questions so that we don’t scare off beginners with you know, these very technical conversation which are also useful to members but can rather be intimidating if you just log in and start.
And yes, this will… if you look at the Live Answer… Jared, if you look at the Live Answers section of the site, you will see that we post videos of every question and answer that happens in these Live Answers. So on Monday of next week, you’ll see this… video of this for example.
Okay anyway, back to this, we’ve got our arguments, the post type and we’ve done our taxonomy query. And this is a very simple taxonomy query so it seems like there’s a lot of stuff you have to do in order… essentially for you to say, “Get me in taxonomy location, this location name.” But it’s got a lot of stuff in it because it can be so much more powerful than it used to be able to be.
Okay anyway, we leave our query alone and if we save this and if we upload it and then if we refresh it… okay so now, we have audiobooks and we’ve got these audiobooks. Well actually, it looks like… yeah, we’ve got these audiobooks and then at some point, we got past the audiobook right here and unfortunately, because of the styling, what’s happened is the name of the next line, Bible and Reference has jumped up there which of course, is not exactly what we want it to do. But nevertheless, now, we don’t have that repetition because now, it is actually just taking those catalog items that are in those specific locations rather than what it did before which was display the first 10 catalog items every time.