Welcome to another part of our “Mastering WP_Query” Series. How have we been doing so far? I Hope you’re enjoying these tutorials as much as we enjoy creating them. In this lesson, we’ll delve into the properties and methods of the WP_Query class. But first, let’s briefly discuss what “properties” and “methods” are.
In PHP, there’s an eleGANt way to write code called Object-Oriented programming (OOP). In OOP, we use “classes” as blueprints – a term borrowed from Jason Lengstorf’s classic post “Object-Oriented PHP for Beginners.” Once you grasp the concept of PHP classes, “properties” and “methods” become quite strAIghtforward, as they’re merely synonyms for “variables” and “functions.” Yes, properties are variables within a PHP class, while methods are functions.
Now that we’ve introduced what they are, let’s explore each property and method.
**Warning:** It’s not advisable to modify properties directly. As the Codex suggests, you should interact with them using WP_Query’s methods.
**Properties of the WP_Query Class**
1. **Query String: $query** This property stores the query passed to the `$wp_query` object.
2. **Query Variables Array: $query_vars** This property holds an associative array of the variables (and their values) for the `$query`.
3. **Queried Object: $queried_object** The property stores the current query object, like the `$post` object for a post query or the `$author` object for an author query.
4. **Queried Object ID: $queried_object_id** This property stores the ID of the queried object.
5. **Posts Returned: $posts** The property contains the posts returned by the query.
6. **Number of Posts Displayed: $post_count** This property stores the number of posts in the current query.
7. **Total Posts Found: $found_posts** The property holds the number of posts found without the LIMIT clause in the SQL query.
8. **Pages: $max_num_pages** This property calculates the number of pages by dividing `$found_posts` by `$posts_per_page`.
9. **Current Post Index: $current_post** The property stores the index number of the current item in the loop. For instance, it starts at -1 and is incremented by `next_post()`.
10. **Current Post: $post** This property holds the current post.
11. **ConDiTional tags Booleans: $is_{conditional}** These properties store boolean values providing information about the current post status, such as:
* `$is_single`: Checks if it’s a single post of any post type (excluding “attachment” and “page”).
* `$is_page`: Checks if it’s a page.
* `$is_archive`: Checks if it’s an archive page.
* `$is_preview`: Checks if it’s a post preview.
* `$is_date`: Checks if it’s a date-based archive page.
* `$is_year`: Checks if it’s a year-based archive page.
* `$is_month`: Checks if it’s a month-based archive page.
* `$is_time`: Checks if it’s an archive based on time (hour, minute, or second).
* `$is_author`: Checks if it’s an author archive page.
* `$is_category`: Checks if it’s a category archive page.
* `$is_tag`: Checks if it’s a tag archive page.
* `$is_tax`: Checks if it’s a taxonomy archive page.
* `$is_search`: Checks if it’s a “search results” page.
* `$is_feed`: Checks if it’s a feed.
* `$is_comment_feed`: Checks if it’s a comment feed.
* `$is_trackback`: Checks if it’s a pingback.
* `$is_home`: Checks if it’s the blog’s homepage.
* `$is_404`: Checks if it’s a 404 error page.
* `$is_comments_popup`: Checks if it’s a comment popup.
* `$is_admin`: Checks if it’s the admin panel.
* `$is_attachment`: Checks if it’s an attachment.
* `$is_singular`: Checks if it’s a single post of any post type (including “attachment” and “page”).
* `$is_robots`: Checks if it’s a query for robots.txt.
* `$is_posts_page`: Checks if it’s the “Posts Page” (set in the Reading Settings in the admin panel).
* `$is_paged`: Checks if it’s a paged query and not the first page.
**Methods of the WP_Query Class**
1. **init()** This method initializes the object and sets all properties to NULL, 0, or FALSE.
2. **parse_query($query)** This method parses the query using the `$query` property and fills all other properties (except `$posts`, `$post_count`, `$post`, and `$current_post`).
3. **parse_query_vars()** This method parses the query variables.
4. **get($query_var)** The method retrieves a given query variable.
5. **set($query_var, $value)** This method sets a given query variable to a specific value.
6. **&get_posts()** This method returns the requested posts and populates the `$posts` and `$post_count` attributes.
7. **next_post()** This method increments the `$current_post` index and moves to the next post in `$posts`, returning the current post object. (This method must be used within a loop to work correctly.)
8. **the_post()** This method uses the Data from the next post to set the global `$post` variable. (This method must be used within a loop to work correctly.)
9. **have_posts()** This method checks if there are any posts to process and returns FALSE if there aren’t. (This method must be used before a loop to function properly.)
10. **rewind_posts()** This method simply resets the `$current_post` and `$post` attributes.
11. **query($query)** This method calls its two sibling methods: `parse_query()` and `get_posts()`, and returns the result of `get_posts()`.
12. **get_queried_object()** This method returns the object being queried. (If not set, it sets `$queried_object`.)
13. **get_queried_object_id()** Similar to the above, this method returns the ID of the queried object. (If not set, it sets `$queried_object_id`.)
**Quick Tip:** If a method has an ampersand (&) symbol before it, it means the method returns by reference.
I hope this clarifies the significance of “properties” and “methods” within a class. If you understand the purpose of the WP_Query class’s properties and methods, I’d say my job here is well done!
Do you have any additions to this article? Please share your thoughts in the comments below. Don’t forget to share this article with your friends if you found it helpful. Until the next part of the series, goodbye!