Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
FREELessons:11Length:1.2 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

5.2 Creating a Loop With get_pages and get_posts

In this lesson, I’ll show you how to use the get_pages and get_posts functions to create a custom loop.

Related Links

5.2 Creating a Loop With get_pages and get_posts

Hello, and welcome back to the Tuts+ course on working with the WordPress Loop. In this part of the course, I'm gonna show you one way to create a completely custom query. And that's using the get_posts function, which I introduced to you in the previous part of the course. Now, as you might guess, you use this to get a list of posts of the post post type. So I'm gonna show you how we can add that to our fame. I'm gonna add a list of the latest posts at the bottom of every post in my site, so it'll appear down here. Now let's take a look at where we add that to our code. So single posts are using the single.php template file, which in turn is using the loop-single include file. Now I don't want to write this within our existing loop. I want to create a completely new loop outside the loop, so I'm gonna close that loop-single file and use the single .php file instead. So I'm gonna add some code underneath the content div. The first thing I do is call a global post variable and set up the arguments for the latest post function. Once I've done that, I want to use the get_post function to get that list of posts using my arguments. And I'll add those to a variable I'll call latestPosts. The next thing to do is check whether that get_post function has actually found any posts. So if that is the case and there are some posts, I then need to write the code to output them. And I like to make sure I've got all my opening and closing PHP tags and my brackets sorted as I go along. I've also opened and closed this div. So I've got h3 with the word latest post, which is translatable. And then I'll create an unordered list, within which I'll use a forEach loop to call each of the posts. So I've got my blevel post variable, my arguments, my get_post function, and then my check if that's returned anything. And then I'm opening a div with the class latestPosts, which I can use for styling. I've got an h3 in there, and then an unordered list using a forEach loop to loop through all these posts. I just realized I need to open php there. So I'm setting up the post data for the post variable, which I've defined for the individual post as I run through each one. And then for that I've got a list item, a link to its permalink, and its title. So I'll save that, and let's take a look at the site. And there it is. So we have a list of the latest posts, which I've coded in my template file using the get_posts function. The get_pages function works in a very similar way. You'd also use a forEach loop, and define the arguments before running the get_pages function itself. And if you take a look at the Codex page, that'll show you exactly what arguments you can use in the parameters. You could use this function in lots of different ways, so you might not want to just collect the latest posts. You could also collect all the posts in a given category or tag, or you could collect posts using other parameters and arguments. And if you want to know what arguments you can use, take a look at the Codex page, which lists all the arguments you can use for get_posts. So that's how you use get_posts to create a completely custom query in WordPress. In the next part of the course I'll show you another way to create a custom query and that's using the WP query class. See you next time, and thanks for watching.

Back to the top