Check out a free preview of the full JavaScript for WordPress course:
The "Query Arguments" Lesson is part of the full, JavaScript for WordPress course featured in this preview video. Here's what you'd learn in this lesson:

Zac illustrates how to passing arguments to API URLs to customize the JSON responses.

Get Unlimited Access Now

Transcript from the "Query Arguments" Lesson

[00:00:00]
>> Zac Gordon: Another thing that we could do, is we could pass arguments into the API URLs and customize what we get back. So what I mean by that is, let's say we're gonna get all the pages on our site. All right, this is a duplication, sorry. Let's say we're going to get all of our posts here.

[00:00:19] If we were to add this query string, now this would all appear in one line. I'm just doing it to break it up and show you the possible order. Now what we're gonna get is limited to ten pages. It's going to have embedded data. So this makes an extra query and it pulls in even more data associated with that post.

[00:00:40] We're going to find things that only have the search phrase, hello world. And I wanted to show this as an example. If you put a space bar, or a space in the URL as we're typing it in, it'll actually work. And if you put this %20, which anybody who's tried to put a space in a URL, you know what that does, that will be valid spaces and work for you.

[00:01:00] So you could search for certain posts. And then another big one that's really common is orderby and order. So order is only gonna be ascending or descending. And orderby could be like titles, it's date by default. It could be other things as well. So, it's worth taking a look over at the documentation and let me just start at the homepage for the rest documentation.

[00:01:29] This is stuff you're gonna wanna get familiar with. You're gonna wanna bookmark it or you know put it up there because there is a lot of data in here that is important for you to know. And particularly when you are first starting off, remember how I showed you there were various endpoints that you could access.

[00:01:44] Well, here they are and here is the documentation on all of them. So if I wanted to find out what arguments I can pass to getting a list of posts. I can click on argument and you could see now, how many per page, search, after or things publish after certain date, publish by a certain author, not publish by an author.

[00:02:09] Before a certain date, so you can get a date range, right, if you pass before and after dates. You could exclude certain things, include certain things, offset order. Offset is helpful for pagination, right? So you could say, get ten, and then the next time give me the next ten after the first ten.

[00:02:26] The order, the orderby, get something by slug, by certain status, categories, tags, basically anything you'd want to query something by, we could do that. So when we talk about how to get data from the WordPress REST API, writing in AJAX reuest, is pretty simple at this point, right?

[00:02:45] There's a bunch of different tools we could use for it. So it's really gonna be a matter of getting comfortable with the different arguments that are possible to get us exactly what it is that we're looking for. And we don't have enough time today. We're gonna play around with passing some of these just so you get comfortable with it, but you really need a lot of time because not only are there posts, but then there's media.

[00:03:08] And media could have slightly different arguments that can be passed to it, just like comments are gonna have different arguments. So this will be something you will at a certain point if you decide to go forward with the WordPress REST API, and building apps with it or projects.

[00:03:25] Use this documentation. Take the time to explore what's possible. And the cool thing is, we could do all this from the browser. So it's pretty simple to play around with. Or if you have other tools that you like, like Postman. I'm not gonna be using that here, but that's a pretty common one for exploring APIs.

[00:03:41] We could pass as many of this that we want, but I do wanna draw your attention again to embed. Notice how it doesn't follow the same format as the rest of them. It could be chained in the same way but embed is just _embed, it takes no argument or value.

[00:03:58] Or it takes no value and what that is going to do we'll look at in a second and you'll see that it gets even more data. But remember that the more data you try to pull in, the more expensive that query, the more potential time you maybe waiting.

[00:04:10] And way way down the road outside the scope of this, there are discussions around, okay, can we even cash certain API endpoints to make them even faster? And how do we go about the best way of doing that? So once you get the data, that's one thing. And then you could worry about making it even more performant.