Advertisement
  1. Code
  2. WordPress
Code

Mastering WP_Query: Series Finale

by
Difficulty:BeginnerLength:MediumLanguages:
This post is part of a series called Mastering WP_Query.
Query Improvements in the Latest Versions of WordPress
Welcome to the end of the "Mastering WP_Query" series. It's been a long journey—this was one of the longest series in the history of Tuts+. Rachel and I are extremely proud to be the two writers of this WP_Query reference.

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:

  • what WP_Query is 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_Query that 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 WP_Query class

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 TRUE if there are posts returned in the query
  • the_post(), which turns the cogs in the loop
  • setup_postdata(), which sets up the global $post variable
  • rewind_posts(), which clears the current loop
  • wp_reset_postdata(), which resets the global $post variable
  • 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!)

Every WP_Query-related filter was (in some way) about the SQL commands that run the query—most of them filter the different clauses like LIMIT, WHERE or DISTINCT. The three actions, on the other hand, serve different purposes:

  • The pre_get_posts action interferes with the query before it's run.
  • The parse_query action handles the parsing of the query.
  • The the_post action 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:

  • $query
  • $query_vars
  • $queried_object
  • $queried_object_id
  • $posts
  • $post_count
  • $found_posts
  • $max_num_pages
  • $current_post
  • $post
  • 25 properties that start with $is_, which work like Conditional Tags

And the methods (functions) were:

  • init()
  • parse_query()
  • parse_query_vars()
  • get()
  • set()
  • &get_posts()
  • next_post()
  • the_post()
  • have_posts()
  • rewind_posts()
  • &query()
  • get_queried_object()
  • get_queried_object_id()

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 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:

  1. listing an author's posts in the current year
  2. creating a "Latest Posts from This Category" which excludes the current post
  3. building a "Most Popular Posts" list, ordered by comment count
  4. creating a simple slider
  5. showing a random quote in the sidebar
  6. listing products between a price range
  7. building a shortcode to show a post inside a post
  8. listing current scheduled posts (with excerpts, if you like)
  9. displaying a "Post from a Year Ago Today" section
  10. showing the children of the current page

I have to admit, this was the most fun part that I worked on!

Part 14: Combining WP_Query With the Main Query

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:

  1. The first example was about displaying only posts of one post type on a category archive.
  2. 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.
  3. 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:

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.

Cheers!

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.