Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this final video of the stage, we look at WP_Query and how to create custom loops in WordPress.
Reference
-
0:00
The loop that we looked at in the last video is highly customizable
-
0:04
using something called WP_Query.
-
0:06
For example, when you're working with custom post types or
-
0:09
custom themes, you'll often see WP_Query in use.
-
0:13
And it's super helpful to understand how it works and
-
0:16
see how to customize it a bit.
-
0:18
Let's start off looking at the codex documentation for WP_Query and
-
0:22
find out different ways that we can customize the loop.
-
0:26
Then we'll look at WP_Query in action, and
-
0:29
see how we might want to use it to customize a theme.
-
0:33
There's a great page on the WP codex on WP_Query.
-
0:39
It starts off with talking about what WP_Query does, and
-
0:43
showing the standard loop and a few alternative ways of doing it.
-
0:47
It then goes through and
-
0:50
it shows all of the different types of parameters that you could use to customize
-
0:55
what you want that loop to display and what conditions it should meet.
-
0:59
For example, there are author parameters, if you just want to display things from
-
1:04
certain categories Whether you want to display certain post or page parameters,
-
1:09
status parameters, custom field parameters and a lot of other ones.
-
1:15
We'll scroll through briefly just to get an idea of some of what's available.
-
1:19
However, it's good that you come back on your own and
-
1:22
look at this in more depth to get an idea of what's really possible.
-
1:27
We see here that we can choose author names or author IDs.
-
1:33
Certain categories.
-
1:37
Certain tags, taxonomy parameters.
-
1:42
You could even include a search parameter, a specific page post slug or post ID.
-
1:53
Whether or not it has a password set, what post types are available.
-
1:59
And we'll pause real quick here to look at the template in action, and
-
2:03
then we'll come back and look at a few more of these.
-
2:06
What I did on my site was I set up a custom page and
-
2:09
by the Template option I chose Custom Template.
-
2:13
I also went under Widgets.
-
2:18
And made sure that I had a Pages widget so
-
2:20
that I can navigate around my site a little bit.
-
2:24
I could then come to my custom page and
-
2:26
I could see that it's outputting three different blog posts.
-
2:31
These are the three posts that I have saved as posts in the admin area.
-
2:35
If we come into our theme folder now and we open up page-custom.php,
-
2:39
we could see up top here that I named the Template Name Custom Template.
-
2:45
This is how WordPress knows that we're dealing with a custom template.
-
2:50
And then down here,
-
2:51
I started a new WP_Query preceded by the arguments I wanted to set.
-
2:58
So we have the arguments, the call to a new WP_Query, and then we have our loop
-
3:04
customized to have_posts to check if there's posts for this new query.
-
3:11
And then while there are results of this new query,
-
3:14
echo them out inside of a list item.
-
3:16
And we're not doing anything if there's nothing found.
-
3:20
One important thing about working with WP_Query,
-
3:23
especially when you use multiple queries on one page, is that you want to reset
-
3:28
the post data afterwards in case there are other loops that it may interfere with.
-
3:35
So what we have here is post_type post.
-
3:38
And on our site we can see that it's displaying posts.
-
3:42
If we change this to post_type page,
-
3:46
we should see that on our site we're now echoing out a list of pages.
-
3:51
At this point we only have one, but
-
3:52
if we added more we'd see more of them linked here.
-
3:57
We can come back to post.
-
4:01
And then let's come back into our WP_Query page on the WP codex, and
-
4:06
look at a few more customizations that we can make.
-
4:11
It's also important to note that if you have custom post_types,
-
4:16
such as an events or portfolio one,
-
4:18
this is the call that you would make in order to display custom post_types.
-
4:23
We could then see that we could customize by post_status, pagination
-
4:28
parameters, and a very important one that we have here is posts_per_page.
-
4:34
This is what we would use to set how many posts we want to display.
-
4:41
So if we want to add another parameter we would put a comma,
-
4:46
posts_per_page, and then the number 1, for example.
-
4:52
And now when we test our page we would see that there's only one displaying.
-
5:01
We could also customize the order that these are displaying.
-
5:06
So we could see order ascending and descending.
-
5:10
Let's try changing this.
-
5:18
Now a quick note about what I did.
-
5:20
You could see when I wrote a number, I didn't have to put it inside of quotes
-
5:24
because PHP can handle numbers on their own.
-
5:27
However, if I want to write out some characters, what's referred to as a string
-
5:32
in WordPress, I have to wrap it inside of parentheses, single or double.
-
5:37
Again, this is something you would pick up more when you study PHP on its own.
-
5:42
But I just want to mention that here because you will see when you're working
-
5:45
with custom post_types that if the value is just going to be a number,
-
5:49
you don't put it inside of quotes.
-
5:51
But if it is a text value or string, then you do have to put it inside of quotes.
-
5:56
So now if we come back and we refresh, we can see we have Hello world, Universe,
-
6:01
Treehouse.
-
6:03
And if we try descending, we have a different order.
-
6:08
We could also change the order by which they appear, for example,
-
6:11
by ID, by title, by post name.
-
6:15
So let's try adding one of these in.
-
6:22
And let's do by title.
-
6:24
And we could see we have w first.
-
6:27
If we were to change that to ascending, then we would see that change.
-
6:33
We could also output random orders or
-
6:36
by parent as well as a whole lot of other options.
-
6:39
But determining what order you want things to appear is pretty common.
-
6:45
And you can see it has examples here of popular posts by orderby
-
6:50
the number of comment_count.
-
6:52
If you have prices as a custom value you could
-
6:56
do a customized output here of how you want them to appear by price.
-
7:03
And then we can have date parameters.
-
7:05
For example, let's say that we just wanted to have something that appears
-
7:11
on a specific date.
-
7:13
We could do that a certain way, for example, setting the year, month and day.
-
7:18
And we're seeing this written as an array with an and
-
7:21
sign in between because this value is going directly here.
-
7:27
However, this is a more common way of writing them out, and you would simply
-
7:30
take each one of these and put them on their own line in this fashion,
-
7:36
separating them by commas and leaving off the comma in the last one.
-
7:41
Again, writing arrays is something you would study in more depth as you study
-
7:45
PHP on its own.
-
7:46
However, we'll point it out here because it's something pretty common you
-
7:49
need to know when working with WP_Query.
-
7:53
So this list goes on and on, even allowing you to search by custom fields, and
-
7:58
doing quite a lot to customize how your content is outputted.
-
8:03
We'll stop here for now.
-
8:04
However, I encourage you to continue to look at these in depth so
-
8:08
that you can get an idea of what is possible when working with WP_Query.
You need to sign up for Treehouse in order to download course files.
Sign up