Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Calling the get_post method of the WP_Query class will essentially just get you the same data that's on the main page of your blog. However, there's a lot more customization you can do, in terms of applying filters to the data you get back. You can filter by category or title or author. You can get sticky posts or a specific page or any number of other queries. This can be done on a default loop using a special method called query_post, and this is a function that's built into WordPress. The same parameters are used when we call on our own WP_Query class, so if you look in the Codex you can see the parameters that it will take.
If you are going to use the query method, you can call it on your object instead of get_post, and you can pass to it a special query parsing object, and inside of it, it is going to have what looks like a URL parameter, and that URL parameter will have the data you want to filter by. So you start with an ampersand and you say posts_per_page, which is one option. And again, these options are listed on the query post, referenced in the Codex. So a posts_per_page we will set equal to five, and then separate each one with an ampersand, and we'll say we want to orderby=comment_count, and then we want to order in descending order.
So before I save it, let's take a look at our original query. So you can see it's in the order of what comes first. So I have this "Restaurant View," "Update Today," and "Hello World!" However, none of these have comments, and really, if I want to make this about displaying comments, the ones that are the most active, then I am going to want to order by comments. So that's what I added into here by ordering it in descending order by comment count. So when I save this and go back and refresh it, you will see that Hello World! comes up top because this one is the most commented on.
So we can actually change the title of this. Instead of saying Posts on page, we can change this to say Top Posts, because now it's accurately reflecting the top posts. An even easier way is instead of writing your options in the query method, when you create your WP_Query object, you can pass them in as an array of associative objects. So we will say array and we'll say, posts_per_page=5, then I like to keep them lined up in this way, and we will say 'orderby' => 'comment_count', and the order is descending.
So now we don't have to call the query method at all. When I initialize this WP_Query object, it will actually populate our query based on these parameters. When I refresh, it doesn't change, although our title changed because we change that. But the order is the same because it's applying that filter. The other option is sticky posts. Sometimes you want to mark a post as sticky, and a lot of theme supports sticky posts to highlight them as if they are headlines, or things like that. They allow you to have some posts that are treated specially.
In order to make them sticky, the easiest way is to go into the main Posts page, in the Quick Edit, there's an option to make the post sticky. When you update it, you'll see on your post that this is a sticky one, so you can quickly review. If I want to filter by it, I simply add another parameter. There is a special parameter called 'post__in' which says I want to get all the posts that are in a specific list. We are going to get a list of IDs using the sticky posts option, and it's stored in the option database.
So we use the get_option method to call on it, and the name of it is sticky_posts. We will now save it. It will now only report those items that are sticky. So when we see it, you can see I now only get "Hello World!" because that was the only sticky one that I had. So as you can see, the WP_Query class gives us a lot of options for filtering our loop data. We can control how many posts we are going to get, we can control the order, and we can filter by category, author, title, or sticky posts, as well as a number of other options.
The way that we set this up is we can either pass our query data into the WP_Query constructor or into the query method. We can pass them as an associative array of elements, or we can also pass them as a URL variable string. However we do it, this provides us with a lot of flexibility in how we output our posts.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92628 Viewers
82 Video lessons · 104167 Viewers
71 Video lessons · 75733 Viewers
56 Video lessons · 107119 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.