本文目录
[隐藏]
- 1What Is WP_Comment_Query?
- 2Properties and Methods of the WP_Comment_Query Class
- 2.1Properties of WP_Comment_Query
- 2.2The Only Method of WP_Comment_Query
- 3Parameters of the WP_Comment_Query Class
- 4A Quick Example to Understand How WP_Comment_Query Works
- 5Wrapping Everything Up
本文是《掌握 WP_Query》系列教程的第 16 部分,该系列共包含以下 19 个部分:
- 掌握 WP_Query : 入门介绍
- 掌握 WP_Query:教你使用Loop循环
- 掌握 WP_Query:相关的函数
- 掌握 WP_Query:行动器和过滤器
- 掌握 WP_Query:WP_Query类的属性和方法
- WP_Query 参数:文章、页面和文章类型
- WP_Query 参数:分类和标签
- WP_Query 参数:分类法(Taxonomies)
- WP_Query 参数:自定义字段(Custom Fields)
- 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
”这个系列的结尾了,是时候介绍 WP_Query 的兄弟姐妹类了,在上一个部分,我们介绍了 WP_User_Query ,下面我们来学习 WP_Comment_Query 。
注:由于时间精力有限,本教程没办法翻译分享,希望朋友们可以加入我们,帮助我们进行翻译,有小酬谢,有意者请联系倡萌QQ 745722006(注明:教程翻译)。
以下为原文:http://code.tutsplus.com/tutorials/mastering-wp_comment_query–cms-23238
We’re almost coming to the end of our series, “Mastering WP_Query
“, and it’s time to introduce the siblings of the WP_Query
class. In the previous part, we went over WP_User_Query
, and in this article, we’re going to learn about the WP_Comment_Query
class.
Let’s begin!
What Is WP_Comment_Query?
Introduced in WordPress version 3.1, the WP_Comment_Query
class does almost all the heavy work on querying comments in WordPress. It allows the querying of two database tables, wp_comments
and wp_commentmeta
, in essence.
Here’s the skeleton of a comment query loop using the WP_Comment_Query
class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php $args = array( // Arguments for your query. ); $comments_query = new WP_Comment_Query; $comments = $comments_query->query( $args ); if ( $comments ) { foreach ( $comments as $comment ) { // Do what you do for each comment here. } } else { // Display message because there are no comments. } ?> |
Pretty easy, right? We’ll get to do an example just a few steps later, but let’s see under the hood first.
Properties and Methods of the WP_Comment_Query Class
Since there aren’t many properties (public variables of the class) and methods (public functions of the class), I’ll quickly go over them in two mini-sections. Here we go!
Properties of WP_Comment_Query
Unlike WP_Query
which has more than 30 properties (25 of them being the equivalents for Conditional Tags), the WP_Comment_Query
class has only five properties:
$request
: A string that contains the SQL query.$meta_query
: An array to make a “meta query” with the help of theWP_Meta_Query
class.$date_query
: An array to make a “date query” with the help of theWP_Date_Query
class.$query_vars
: An array of the variables of the query.$comments
: An array of comments fetched with the query.
The Only Method of WP_Comment_Query
Yep, there’s just one method to use with the WP_Comment_Query
class, and that method’s name is query()
.
The query()
method basically executes the query, using the parameters which we’ll be going through in the next section; but let’s see what we get in an array when we use this method:
comment_ID
: The comment’s ID.comment_post_ID
: The post’s ID which the comment is made to.comment_author
: The comment author’s name.comment_author_email
: The comment author’s email address.comment_author_url
: The comment author’s website URL.comment_author_IP
: The comment IP address.comment_date
: The comment date.comment_date_gmt
: The comment in GMT time format.comment_content
: The content of the comment.comment_karma
: An unused database field for each comment—plugins can use this to store, well, the comment’s karma.comment_approved
: The comment’s approval status.comment_agent
: The comment author’s user agent.comment_type
: The comment’s type if it’s apingback
or atrackback
.comment_parent
: For nested comments, this is the parent comment’s ID. If it’s the top-level comment, this will be0
.user_id
:0
if the comment author isn’t registered with the website, the ID of the user otherwise.
Let’s see the parameters of the WP_Comment_Query
class now.
Parameters of the WP_Comment_Query Class
There are 34 parameters we can use with WP_Comment_Query
, but don’t let them scare you: You can already recognize them from their names, and the others are equally easy to explain and use.
author_email
(string): Comment author email address.author__in
(array): Author IDs to include in the query.author__not_in
(array): Author IDs to exclude from the query.post_author__in
(array): Same asauthor__in
.post_author__not_in
(array): Same asauthor__not_in
.include_unapproved
(array): An array of user IDs or email addresses whose comments should be returned regardless of their approval status.fields
(string): Comment fields to return. Accepts'ids'
only, used to return only the comment IDs.comment__in
(array): Comment IDs to include in the query.comment__not_in
(array): Comment IDs to exclude from the query.karma
(integer): The “karma” score to return matching comments for. (Remembercomment_karma
from the previous section?)number
(integer): The maximum number of comments to return.offset
(integer): The number of comments to pass over in the query.orderby
(string or array): A comment status or an array of statuses to order the query results. Accepts all the keys returned from thequery()
method, plus'meta_value'
,'meta_value_num'
, value of$meta_key
,FALSE
, empty array or'none'
. (The last three disable theORDER BY
clause in the query.)order
(string): How to order retrieved comments—'ASC'
for ascending or'DESC'
for descending. (Default:'DESC'
)parent
(integer): Parent comment’s ID to retrieve children.post_id
(integer): Post ID to retrieve comments. (Default:0
)post__in
(array): Post IDs to include in the results.post__not_in
(array): Post IDs to exclude from the results.post_author
(integer): Post author’s ID to limit results by.post_name
(string): Post slug to get comments from.post_parent
(integer): Parent post ID to get comments from.post_type
(string): Post type to get comments from.post_status
(string): Post status to get comments from.status
(string): Comment status to limit results by. Accepts'hold'
,'approve'
,'all'
or a custom comment status. (Default:'all'
)type
(string or array): A comment type or an array of comment types to filter the query. Accepts'comment'
,'pings'
(meaning pingbacks and trackbacks combined), or custom comment types.type__in
(array): Comment types to include in the query.type__not_in
(array): Comment types to exclude from the query.user_id
(integer): User ID to include comments of a specific user.search
(string): Search terms to get matching comments for.count
(boolean): Return the comment count (TRUE
) or an array of comments (FALSE
). (Default:FALSE
)meta_key
(string): A custom meta key to include matching comments only.meta_value
(string): A custom meta value to include matching comments only.meta_query
(array): An array ofWP_Meta_Query
clauses (which we’ll see in the next part of this series).date_query
(array): An array ofWP_Date_Query
clauses (which we’ll see in the next part of this series). (Default:NULL
)
Note: The default values of all parameters are empty, unless stated otherwise above.
A Quick Example to Understand How WP_Comment_Query Works
It wouldn’t feel like a complete tutorial if we didn’t see how it works, would it? Let’s think of a simple scenario and do a quick example, then.
Let’s say that you’re going to list the comments made by the post’s author and order the list by comment IDs (instead of comment dates). Here’s what you do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php // Get the global `$wp_query` object... global $wp_query; // ...and use it to get post author's id. $post_author_id = $wp_query->post->post_author; // Setup arguments. $args = array ( 'user_id' => $post_author_id, 'orderby' => 'comment_ID' ); // Custom comment query. $my_comment_query = new WP_Comment_Query; $comments = $my_comment_query->query( $args ); // Check for comments. if ( $comments ) { // Start listing comments. echo '<ul class="author-comments">'; // Loop over comments. foreach( $comments as $comment ) { echo '<li>' . $comment->comment_content . '</li>'; } // Stop listing comments. echo '</ul>'; } else { // Display message if no comments are found. echo '<p class="no-author-comments">' . __( 'The post author didn\'t post any comments.', 'tutsplus' ) . '</p>'; } ?> |
Quick Tip: If you want to build comment queries but want to use a GUI instead of typing code, you can useGenerateWP’s WP_Comment_Query Generator.
Wrapping Everything Up
As I said, we’re coming to the end of this series. In the next part, we’re going to learn about theWP_Meta_Query
and WP_Date_Query
classes together.
阅读该系列的其他文章: 上一篇:掌握 WP_User_Query 下一篇:掌握 WP_Meta_Query 和 WP_Date_Query