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

5.1 Methods for Creating Custom Loops

Hello, and welcome back to this Tuts+ course on working with the WordPress Loop. In this part of the course, we're gonna get started on creating completely custom loops and queries. And I'm gonna start by showing you what the different methods are that you can use to do this, and telling you which ones you can do, which ones you should use in different circumstances. So let's take a look first at the get pages function. Now, this is a template tag that you can add in your theme template files, but you can add it in a plug-in or in an include file such as your loop file. This uses the WP query class to actually fetch a list of posts. But it simplifies things because it's a function that's been developed for you to allow you to specifically get the post, post type, and to add some simple parameters. So I'll demonstrate to you in the next part of the course how you use the get posts template tag. But it's a good way to fetch a list of, say for an example, the most recent posts or posts within a certain category. And it can mean less work than using the fold up UP query class. The get pages function works in a very similar way to get posts, but yes, you guessed it, it fetches pages on your site instead of posts. It has some slightly different parameters from get posts, and you need to check those before you use it because obviously some of the attributes of a page might be different from the attributes of a post. But again, it's sometimes a simpler way to get a list of pages in your site without having to use the fold up vp query class. So for example, I would use it to get a list of the pages that are a child of the current page or to get a list of the top level pages in your site. The third, and the most powerful, method is to use the WP Query Class. Now, this will allow you to write whatever custom query you want. It's incredibly versatile. It lets you query different objects, different post types. It lets you query taxonomy terms, anything you can think of. Authors, absolutely anything that can be stored in the database in WordPress, you can query it using WP Query. It's quite big though, and it's quite unwieldy to get your head around. So if you want to get to know WP_Query better, I recommend looking through the series of tutorials on the Tuts+ website, which shows you how to understand all the different aspects of WP_Query, and all the things that you can do with it. So I'll also show you later on in this course how to use WP_Query to create your own custom query. A fourth method is the query_posts method, which you might sometimes see if you Google create my query or write a custom query. People have been known to put query_posts up as the way to do it. But beware, query_posts is not designed, it's not intended for use by plugins or themes. It will completely override the main query, and it could slow your site down because it throws everything out that the main query has done. So that's the main query that we've been working with in the standard loop and with pre_get_posts. It throws all that out and starts all over again. So it wastes all of that processing that WordPress has already done running the main query. So I'm only including this in order to say to you please don't use query_posts. The advice from the WordPress codex and from every WordPress expert I have ever spoken to is just don't use it in your plug-ins or themes. And then finally, if you'd like to know what happens when you run a query using any of these functions or classes, this info graphic shows you exactly what happens when you run a query using these methods. So those are the main methods of writing a custom query in WordPress. The three that you'll be using are get_post, get_pages and WP_Query. And I'm gonna show you get_pages and WP_Query in the next two parts of the course. See you next time, and thanks for watching.

Back to the top