掌握 WP_Query:结尾

秋天到了,大榕树结了又黑又小的果子,风一吹,果子掉下来了,就成了我们天然的玩具。
本文是《掌握 WP_Query》专题的第 19 篇,共 19 篇:
  • WP_Query 参数:自定义字段(Custom Fields)
  • WP_Query 参数:分类法(Taxonomies)
  • 掌握 WP_Query : 入门介绍
  • 掌握 WP_Query:教你使用Loop循环
  • 掌握 WP_Query:相关的函数
  • 掌握 WP_Query:行动器和过滤器
  • 掌握 WP_Query:WP_Query类的属性和方法
  • WP_Query 参数:文章、页面和文章类型
  • WP_Query 参数:分类和标签
  • WP_Query 参数:日期
  • WP_Query 参数:状态、排序和分页
  • WP_Query 参数:作者、搜索、密码、权限、缓存和返回字段
  • 掌握 WP_Query:10个有用的例子
  • 结合 WP_Query 与主查询(the Main Query)
  • 掌握 WP_User_Query
  • 掌握 WP_Comment_Query
  • 掌握 WP_Meta_Query 和 WP_Date_Query
  • WordPress 4.1的查询改进
  • 掌握 WP_Query:结尾

欢迎来到“掌握WP_Query”系列的结尾。这是一个漫长的旅程,这是TUTS史上最长的系列之一。 Rachel和我非常荣幸能成为这个WP_Query 参考的两位作家。

我们希望你喜欢这次旅程。在这个最后的部分,让我们一起回顾和总结我们所学习的内容。

注:由于时间精力有限,本好代码教程没办法翻译分享,希望朋友们可以加入我们,帮助我们进行翻译,有小酬谢,有意者请联系倡萌QQ 745722006(注明:好代码教程翻译)。

以下为原文:http://code.tutsplus.com/tutorials/mastering-wp_query-series-finale--cms-23452

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

  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:

  • mastering WP_User_Query in part 15
  • mastering WP_Comment_Query in part 16
  • mastering WP_Meta_Query & WP_Date_Query in 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_Queryclass 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!

到此这篇关于掌握 WP_Query:结尾就介绍到这了。人生就像迷宫,我们用上半生寻找入口,用下半生寻找出口。更多相关掌握 WP_Query:结尾内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

标签: WP_Query