7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

  • Overview
  • Transcript

5.2 Creating a Custom Query

In this lesson, you'll learn how to create a custom query to display a custom post type in your theme. We'll be using variables, conditional tags, and the loop.

Related Links

5.2 Creating a Custom Query

Hello, and welcome back to this tuts plus course on learning PHP for WordPress. In this part of the course, we're gonna move on from our custom post type taxonomy. Which we've already registered and create an archive for the custom post type in our theme. So I've created a file called archive-r Maxi_book.php. And that's the way that you name an archive file for a post type. So you call it archive dash, and then the name of the post type. So this will be used by WordPress to output the archive for that post type. Now at the moment, all that's got is my call to the header, the sidebar and the footer. And I'm using this get sidebar full call here because I've got a specific full dash sidebar.PHP file in my theme, which will display a full width sidebar on full width pages. And this is a full width page. So let's start by closing off PHP and adding some HTML. So I'm gonna add a heading, Books. So now I need to open PHP up again and I'm going to run the loop. And here, I'm gonna be using the standard loop, but I'm gonna run it more than once using the one query and I'll show you as I go along. So that's our opening line, opening up the loop while have post the posts, so far so normal. Now, what I want to do is check if a post has a specific term in my taxonomy that I've already registered. Because first I want to output books from this Division Bell series. So I'm using this function If has_term And that has two parameters. The first one, is the term name. And the second is the name of the taxonomy, which we've already created. And if that's the case, we will then make a few things happen. So we're checking if any posts have that term. And I want to do that before I output heading for this specific part. So I'm going to use a variable to check whether we've got any terms. And that variable is called count. And I'm going to add one to the value of that count. But before the loop runs, I'll need to set up that count variable and set it at zero. So let me explain this. Here we have our count variable, which is set to zero to start. And then if we have any posts with the term Division Bell in that series taxonomy, which isn't our Maxi series, it's just plus series, that's the prefix that I used. If that's the case, the count goes up by one. And it will loop through the post every time it finds one with that term, it will add one to the count. And then we can check for the value of the count. So I'll now use that endwhile to end the loop. And then I run a second loop, which is where I'm actually outputting the posts. So first, I'm checking the count is greater than zero. In other words, if it's found any posts and added one or more to the value of count. First up, I'll rewind the posts. And then I'm going back into HTML and adding a section. And I'm calling it giving it a class of series so I can style those sections. I'll give it a heading, and I'll run the loop again. Making sure I have PHP up again. So now once again, I want to check if the specific post has that term. So I'm using that if has term conditional check again. And if that is the case, I will get a template part that outputs an article with that book in it. And I've already created that template part file which you can see here it's called loop-book.PHP. So if the current post has the term Division Bell in the tut's plus series taxonomy, we then get that template part. And that outputs an article. So let's just run through this quickly, which will be familiar from an earlier part of the course on the loop. So we've got an article with the ID of the posts ID, and then classes which are added by WordPress using the post class function. And then in addition, I'm adding some classes to that, which are styled in the style sheet of my theme. Now you might want to change yours or add some different ones. We've then got an h4 with a link to the post and the title. We've then got the post thumbnail or featured image if there is one, which is output inside a link to the post. And then after that, we have the Exit. And then finally, there's a div with the class of button, which again is starting my theme. And that's got a link to the permalink. And it says, explore the book. So that's that loop file, which will be run again and again as we go through this template file. So now I want to end while again, making sure I type it correctly. So I'm now ending the loop. And then, I'm gonna close that section. So within my second loop, I've got a check for the count rewinding posts. Opening a section, outputting an h3 with the name of the series, and then checking if it's got the term for each post and outputting the contents of the loop. And now I'm gonna repeat that for another two loops. But instead of typing it all out again, I'm gonna copy and paste. So let's start by copying the first loop to create the third loop and I need to start PHP up there. Which my code highlighter showed me I haven't done correctly. Now we have the third loop, we rewind posts first. I'm resetting count to zero again, because again I want to check if it's higher than zero. And then I'm changing the term we're looking for here from Division Bell to the Village. So after that, I'll copy my second loop to make my fourth loop. And I'll change the title. And I've also changed the term that we're looking for. And again, we're outputting that template part. We then need to repeat that for the fifth and sixth loop. So we'll copy both of those and edit them. And here I've got the term other, because if we look back at the site there's another series here called other. And then I'm adding here. I'm adding other books, stories and novellas and changing the term to other. So those are my six loops. Now those six loops, but they only query the database once. If I was to use the WP query class, which is a good way to create a custom loop, but it does mean that you're constantly querying the database. But here I'm using that one query. The WordPress is already sending to the database to find all of the books, all of the posts in my custom post type. So I'm finally finishing off with a closing div, which closes that content div which is opened in my header file and then I'm calling my sidebar and footer. So that's the code for my archive file. Now let's take a look and see how it looks on my site. So you can see it's completely changed. Instead of being that long list with lots of white space, we've got books as our heading. We've got the Division Bell trilogy with the three books next to each other. We've got the Village trilogy with the three books next to each other and a set of stories that I'm included in that. And then I've got other books, stories and novellas, which is another book here which I've set as other. So that's how you create a custom query using the loop, using conditional statements, and also using variables in WordPress. You can take this example, this case study, and adapt it to work with your own custom post types and taxonomies. In the next part of the course, we'll wrap up and look through what you've learned as you've been going through the course. See you next time and thanks for watching.

Back to the top