Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
As we've learned, every page in WordPress has its own loop. This contains the data relevant to that page. At some point, we might want to query our own loop and essentially access the same kind of post and page data, but not have it be whatever is on the current page. There's a special class built into WordPress called WP_Query that we can use for this. If you remember, we've built this top_ posts widget, which will output information about each post that's on the current page. So as you can see, on the default page, it's outputting the two items. But if I go to the About page, it outputs the About page itself, which is stored in that loop.
What it's doing is it's calling these methods, have_posts and the_post, on the special default WP_Query class that's built into WordPress. This class gets its data in the header, and it gets it depending on the file you're in and any URL variables that might have been passed to it. If we want to create our own object, we can essentially create a variable and set it equal to the WP_Query class.
This class is made to store this data, and it's much cleaner and easier and better to use than accessing the database directly. So what we do to populate it is we simply call the get_posts method of that object. This will go out and get posts. You can apply filters if you want to, and we'll talk about that later. For now, it's just going to populate it with the default post data, essentially what would display on the first page: any sticky posts and then other posts in descending order.
To output in our loop, we now need to access the post data from this query. So just change it so you are calling the same methods of the query. So now it's getting data from our posts. The post itself will always populate the default post, and that is what's accessible by these template tags. So you don't have to do any changes to the template tags; just make sure you prefix your have_posts and the_post with your specific post object.
So let's go back to our site, and let's go ahead and refresh the homepage. You'll see I have "Update for Today" and "Hello world!" These are the default posts, which is what I'd expect because those are the post available on the main page. However, if I go into the About page, you'll see that I still have the same posts, because it's now getting these posts from our get_posts call. So, WordPress offers us the ability to create our own customized post and page data by using this WP_Query class.
We can then call the get_posts method to populate it, and we loop through it just like we did with the main loop, using have_posts and ultimately setting the post information to the current page by calling the_post.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98522 Viewers
61 Video lessons · 85812 Viewers
71 Video lessons · 69715 Viewers
56 Video lessons · 102027 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.