Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:11Length:1.2 hours
  • Overview
  • Transcript

4.2 Modifying the Loop Using pre_get_posts

In this lesson, I’ll show you how to use the pre_get_posts hook to modify the standard loop.

Related Links

4.2 Modifying the Loop Using pre_get_posts

Hallo, and welcome back to this Tuts+ course on working with the WordPress Loop. In this past course, I'm gonna show you another way to modify the query, and that doesn't involve editing your template files at all. And this is by using the pre-get post action hook provided by WordPress that lets you modify the main query. And if want to know more about it, you can have a look at the action reference here on the Codex. But let's take a look at the code, and before I do that I'm gonna tell you what I'm gonna do. So here's my project's page which is a custom post type archive that we've already created the template file for. But I want to modify this. And I'm gonna do that using pre-get posts. So instead of the default ordering, which is descending order of time of when a post was published, I'm gonna order it in ascending order of title, so these projects will be listed in alphabetical order. So let's take a look at the code. So there are two ways you could do this, you could either create a plugin for it, or you could add it to your functions file in your theme. Now I could edit to my functions file which would be perfectly valid or alternatively, I could add it to the plugin that I already created, Tustsplus register projects. Now because this is where I registered that project, and without this plugin installed and activated, the post type won't work. I think it's a good idea to put it in here. So I'm gonna take this common text that is already in my function's file and I'm gonna add it to my plugin. Now if you wanted to modify the loop for a standard file type, you might want to add that to your functions file in your theme. But because I'm modifying the loop for a post type, not a file type sorry, a post type that's been registered by this plugin. The best place to put this code is in that same plugin. So let's get started. So my function is called Tutsplus_modify_loop_projects and that has one parameter which is query because this function will need to work on the query object. So that's my function, and then the hook I need to attach to is the pre_get_posts hook. So I'm gonna copy that as my second parameter for add action. At the moment that won't do anything because there's nothing there. So let's populate it. The first thing we need to do is check two things. Check that we're on the main query which you always have to do if you're using pre_get_posts. And also I want to check that I'm on the archive page for my projects post type. Now to do that I use a conditional tag. So here you can see I've got is_post_type_archive, $post_type. Which is the conditional tag that I use. And instead of just using is_post_type_archive, I need to refer that to the query object. So I put if query is_post_type_archive, And then my $post_type. In fact, let's copy and paste that up here. So we know we've got it exactly right. So it's tutsplus_project, And, Query again, is_main_query. So let me just check, take that bracket out. And that surrounds all of my if statement. So if that's the case I'll need to do something, so let's just run through that again. So if the query is for a post type archive of that post type, Tutsplus_project. And it's the main query, which means that I'm in the main query on that archive page and I'm not in an extra query that's been added by a widget or something like that. So if that's the case, we then do something. And what we're doing here is using query set to change some of the ways the query works. Now I need to set two different parameters or two different sets of parameters. One of the is the order the other one is the order by. So I do orderby first. Making sure I type that correctly. So I'm ordering by title, and the best way to find a list of all of the parameters you can use here, is by looking on the WP Query page in the Codex. So this has a list of all the different parameters that you can use in the WP_Query class but you could also use them when using pre_get_posts. So if I find order, here we are, order and order by parameters. So I'm using order ascending and order by title. So those are the two things that I'm changing if we are on that project archive. The orderby title and the order ascending. So let's just take a quick look at how it look before we activate that before we save our file. So it sort of feels random. Although you've got the dates which shows you what order it's in, I think for a project file, you want to be able to order them in the way that you want to. And you could change that if you wanted to, you could do anything here. So the parent or the menu order, you could use the menu order which is commonly used in pages but you could abort your projects if you wanted to and then you could manually alter them in the back-end. But I've done it ascending in alphabetical order. So if I save that and then go back to my site and refresh the screen, you'll see that it's now in alphabetical order. So that's how you use the pre_get_posts hook to modify the query within your loop. In the next part of the course, I'm gonna take things a stage further and show you how to write a completely custom loop to list out the posts in your site. See you next time and thanks for watching.

Back to the top