FREELessons: 12Length: 1.3 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

4.2 Writing the Query

In this lesson, we'll write a query using the WP_Query class to output the latest post in each category.

Related Links

4.2 Writing the Query

Hello, and welcome back to this Tuts+ course on WordPress plugin development. In this part of the course, we're going to continue writing our plugin. To display the letters post in each of three categories, so we now have our get_categories function. Which we've used to fetch three random categories using these arguments here. And we have our WP query class for which we've defined some arguments as well. So now's the time to realm that query. So we're defining our query variable as a new version, a new instance, of the WP query class. With the arguments here that we've already defined, and then we need to check if the query has any posts. So if the query has posts, we now need to run a while loop. And this looks very much like the standard loop, normally we have while, oops, not has post, have post. While have post the post, instead, we have while query have post query the post. Because we're using this query here, this instance, of the WP query class. So now we're running an individual post, so we need to open an article element for it. And, but also need to close our PHP, and then I'm gonna open PHP again, under here for these brackets. So, I'm now am at opening an article with the ID of post-theID which is the post ID. And then with classes that will be generated by the PHP post class function. This is just like the standard loop, and I've also add category post to my classes. In case I wanted to start that later on, I'll make sure my article element is closed. And then within it, I need to add the contents of the loop. So I'm going to add an h3 for my title, and then within that. I'll echo the category name. So that will echo out the name of this category and then I'll close my h3 text. And then I'm going to show the post them now, so I need to check of it has the post them now. And once again, I'm back in PHP. So I'm going to add the post them now inside a link to the post. And you'll notice that I'm back in HTML again. And I think it's a good idea to make sure that every time you close PHP. You open it up again somewhere else below, so that you don't end up with an error being thrown up. Because of the file being closed prematurely, because PHP is quite an unforgiving language. And you have to get it absolutely right. So I've got an A, a link going to the permalink and then inside that, I've got title of the post. And then I've got, closing my link, that's wrong, I don't want the post title yet, just spotted an error. What I want instead is, is the post thumbnail. And I'm gonna have it at medium size. That's my link to the post via the featured image, and then I want to add the title. Then I'll close my link and I want to put that inside an h4 tag. And then underneath that, I'm going to add the exit. So, we have an article element set up as you normally would in the loop. We've got a heading with the category name, we've got a check for the post them now. And if it does have a post them now, we're outputting that post them now. And we then have an h4 with the title followed by the exert, now, there's one thing wrong here. And that's that this conditional tag should only be going around the code for the post thumbnail. So I will remove the other code and add it below. And then, because we've got while query have_posts up here outside our article, we need an endwhile. And we also, after this bracket here, want a reset_postdata. And the WP Reset Post Data function will reset the query so that we can keep running new loops. So that's my query and that's my code to output the content. But at the moment, it won't do anything within my site because I haven't hooked it to any action hooks. Or I haven't coded this function, tutsplus category post listing, into my theme. So let's take a look at my theme files, so here's my theme tutsplus plugin development. And in single dot php, I have a hook called, where is it? It's tutsplus after content up here, so I'm going to add the add action function And I'm going to copy that. As the first parameter, and then the second parameter, will be the name of my function, I'll save that. I go back into my site, and I've tried to activate my plugin, but you can see that there's an error here. So let's have a look at expecting a bracket on line 47, here we go. [NOISE] >> Let's try activating it again, if I go back to my post and I refresh the screen, you can see the latest posts. And there's an undefined function, wp_reseet_postdata and that's because of a typo. So I get rid of that, refresh again, and I've refreshed my post. And you can see that it's querying the same post for both basics and essentials. Because it's going to the category, it's finding the latest post and it's giving it to us, now, I don't want it to do that. So I need to add an array of variables with the post IDs in of those posts that have been output. And that way I can check in my query that the post being output by each query. In turn, is not in the array of variables for the IDs that have already been used. It'll make more sense as we do the code, so first I'm gonna define an array of variables. And we can see here that I've closes PHP there, so I need to close it here instead. So I've got an array of variables called do not duplicate and I've added zero to it. Because that won't be output by any of my posts, and then at the end of each loop. I need to add the ID of the post that's just been output to that array of variables. So I'm adding the ID of the current post to do not duplicate, and then finally, in my WP query arguments. I have to use post and then it's double underscore not underscore in. [SOUND] Do not duplicate, so I'll save that and then go back to my post and you'll see that they're all unique posts. So that will ensure that even if a post is in more than one category. It won't show up in more than one of these instances, so that's how you output the content. In the next part of the course, I'm gonna show you how to add a style sheet to your plugin. And how to use that style sheet to turn this from a list one above the other. To putting them next to each other from side to side, see you next time, and thanks for watching.

Back to the top