Mastering WP_Query: Series Finale
We hope you enjoyed the ride. In this final part, we’re going to go through what we learned and close the curtains.
Part 1: An Introduction
This was the introduction to the series by Rachel. In this part, we learned:
WP_Queryis in simple terms and a simple example
- why we should use
WP_Query, and why we should use it over other methods to create custom queries
- the “caveats” of
WP_Querythat we should know about
Part 2: Using the Loop With WP_Query
The second part was about creating a loop with the
WP_Query class, also written by Rachel. In this part, we learned:
- what a “WordPress loop” is and where it’s used
- the way WordPress loops are structured (with a well-written, detailed example)
- running extra loops with the
Part 3: Related Functions
In this part, written by yours truly, we went over 13 functions that interact with
WP_Query, making it easier to work with it. The functions were:
get_query_var(), which gets public query variables
query_posts(), which alters the main loop and is a bad, very bad function
get_post(), which gets a simple post to work with
get_posts(), which gets posts and returns them in an array
get_pages(), which gets pages in the same fashion
have_posts(), which is a Conditional Tag that returns
TRUEif there are posts returned in the query
the_post(), which turns the cogs in the loop
setup_postdata(), which sets up the global
rewind_posts(), which clears the current loop
wp_reset_postdata(), which resets the global
wp_reset_query(), which resets the whole query
is_main_query(), which checks if the current query is the main query (and is another Conditional Tag)
in_the_loop(), which checks if we’re in the loop (and is yet another Conditional Tag)
Part 4: Related Filters & Actions
This part that I wrote was both annoying and rewarding to write about. It was annoying because it took way more research time, but it was very rewarding because, well, I learned so much. (Pro learning tip: Write a tutorial on a subject that you know little about!)
WP_Query-related filter was (in some way) about the SQL commands that run the query—most of them filter the different clauses like
DISTINCT. The three actions, on the other hand, serve different purposes:
pre_get_postsaction interferes with the query before it’s run.
parse_queryaction handles the parsing of the query.
the_postaction modifies the post object.
Part 5: Properties and Methods
In this part that I wrote, we went over the properties and methods of the
WP_Query class. (“Properties” and “methods” are merely “variables” and “functions” within a PHP class.) The properties (variables) we learned about were:
- 25 properties that start with
$is_, which work like Conditional Tags
And the methods (functions) were:
Parts 6 to 12: Arguments of the WP_Query Class
In the next seven parts, Rachel did a fantastic job explaining all of the arguments that we can use with the
WP_Query class, with little examples:
- Part 6: Arguments—Pages, Posts and Post Types
- Part 7: Arguments—Categories & Tags
- Part 8: Arguments—Taxonomies
- Part 9: Arguments—Custom Fields
- Part 10: Arguments—Date
- Part 11: Arguments—Post Status, Pagination & Ordering
- Part 12: Arguments—Author, Search, Password, Permissions, Caching and Return Fields
Part 13: 10 Useful Examples of WP_Query
In this part, I took the liberty of going through various examples using the
WP_Query class and its related functions. (I also gave a quick example to remind you how to create a loop with
WP_Query.) The examples were about:
- listing an author’s posts in the current year
- creating a “Latest Posts from This Category” which excludes the current post
- building a “Most Popular Posts” list, ordered by comment count
- creating a simple slider
- showing a random quote in the sidebar
- listing products between a price range
- building a shortcode to show a post inside a post
- listing current scheduled posts (with excerpts, if you like)
- displaying a “Post from a Year Ago Today” section
- showing the children of the current page
I have to admit, this was the most fun part that I worked on!
This part, written by Rachel, tells us about combining the
WP_Query class with the main query. It’s a very intriguing subject but I have to give some spoilers here—in this part, Rachel gave three great examples:
- The first example was about displaying only posts of one post type on a category archive.
- The second example was about getting the posts (returned by the main query) by post type and listing the posts separated by post type, yet in a single listing.
- The third example was about separating the main query into two custom queries.
Parts 15 to 18: The Companions of the WP_Query Class
Did you know that there are classes that work like the
WP_Query class to help
WP_Query? In the following parts, Rachel and I talked about:
WP_User_Queryin part 15
WP_Comment_Queryin part 16
WP_Date_Queryin part 17
Part 18: Query Improvements in WordPress 4.1
In this part, Rachel talked about the improvements to the query system of WordPress in version 4.1—like nested queries, comment parameters and general bug fixing. It’s a gentle part, a light read before the end.
Part 19: The End
As the Oracle said in The Matrix Revolutions: Everything that has a beginning has an end.
It was a long ride, and as I said in the beginning, it’s one of the longest series in the history of Tuts+. I joined forces with Rachel McCollin, one of the best instructors in Tuts+, and we’re both proud to be the ones that wrote the longest, the most comprehensive and the most epic tutorial series on the
WP_Query class and its companions.
We worked very hard on the series, and we really hope that you enjoyed every bit of it. If you learned something new in this 19-part series, then we’re happy that we’ve done a good job.
If you have any comments about this series, don’t hesitate to share your thoughts in the Comments section below. And, as always, if you liked the series, don’t forget to share this final part with your friends as an index.