Tìm theo từ khóa

Hiển thị tất cả comment trong web wordpress, các tham biến của hàm WP_Comment_Query wordpress,



Hôm nay nhân lúc cập nhật lại giao diện codfe.com mình muốn hiển thị tất cả comment của website vậy là mày mò lên google search từ khóa “show all wordpress comments” tạm dịch là hiển thị tất cả comment trong web wordpress. Các bài viết cả tiếng Việt và tiếng Anh khá nhiều tuy nhiên hầu hết là ko có hoạt động được. Cũng đúng thôi code cập nhật thường xuyên theo các phiên bản nên code ko chạy là bình thường. Chỉ cần nắm được từ khóa đúng cho vấn đề, tìm lại liệu là giải quyết được.

Sau một hồi tìm kiếm thì từ khóa liên quan đến các hàm lọc comment trong wordpress là WP_Comment_Query mình sẽ giải thích và demo bên dưới nhé!

WP_Comment_Query là gì?

Từ phiên bản wodpress 3.2 hàm WP_Comment_Query  xử lý hết những yêu cầu về về bình luận trong website wordpress. Hàm WP_Comment_Query  sẽ truy vấn dữ liệu trong 2 table chính trong database wordpress là  wp_commentswp_commentmeta.

Sau đây là đoạn code mẫu demo các sử dụng class WP_Comment_Query

Trong đoạn code ví dụ trên có 2 phần, phần 1 là tạo bộ lọc để lọc dữ liệu $args = array…   phần thứ 2 là vòng lặp lọc ra các comment cần hiển thị  từ dòng foreach … tùy yêu cầu mà chúng ta sẽ tạo ra các bộ lọc dữ liệu và hàm hiển thị tương ứng.

 

CÁC HÀM VÀ THUỘC TÍNH CỦA LỚP WP_Comment_Query

Trong phạm vi bài viết này mình chỉ đưa ra các thuộc tính (properties ) và phương thức (methods) phổ biến thôi nhé!

Các thuộc tính của WP_Comment_Query

WP_Comment_Query có 5 thuộc tính như sau:

  1. $request: A string that contains the SQL query.
  2. $meta_query: An array to make a “meta query” with the help of the WP_Meta_Query class.
  3. $date_query: An array to make a “date query” with the help of the WP_Date_Query class.
  4. $query_vars: An array of the variables of the query.
  5. $comments: An array of comments fetched with the query.

Các phương thức của WP_Comment_Query

Class WP_Comment_Query chỉ có một phương thức duy nhất là query().

Phương thức query() thực hiện việc truy xuất dữ liệu, sử dụng các tham số mà mình sẽ liệt kê và giải thích phần bên dưới nhé:

  1. comment_ID: The comment’s ID.
  2. comment_post_ID: The post’s ID which the comment is made to.
  3. comment_author: The comment author’s name.
  4. comment_author_email: The comment author’s email address.
  5. comment_author_url: The comment author’s website URL.
  6. comment_author_IP: The comment IP address.
  7. comment_date: The comment date.
  8. comment_date_gmt: The comment in GMT time format.
  9. comment_content: The content of the comment.
  10. comment_karma: An unused database field for each comment—plugins can use this to store, well, the comment’s karma.
  11. comment_approved: The comment’s approval status.
  12. comment_agent: The comment author’s user agent.
  13. comment_type: The comment’s type if it’s a pingback or a trackback.
  14. comment_parent: For nested comments, this is the parent comment’s ID. If it’s the top-level comment, this will be 0.
  15. user_id: 0 if the comment author isn’t registered with the website, the ID of the user otherwise.

Cùng xem qua các tham số của Class WP_Comment_Query.

Có tất cả 34 tham số có thể dùng trong WP_Comment_Query, vì là rất nhiều tham số như vậy nên mình sẽ liệt kê ra và phân loại bên dưới để mỗi lần sử dụng tiện tra cứu:

  1. author_email (string): Comment author email address.
  2. author__in (array): Author IDs to include in the query.
  3. author__not_in (array): Author IDs to exclude from the query.
  4. post_author__in (array): Same as author__in.
  5. post_author__not_in (array): Same as author__not_in.
  6. include_unapproved (array): An array of user IDs or email addresses whose comments should be returned regardless of their approval status.
  7. fields (string): Comment fields to return. Accepts ‘ids’ only, used to return only the comment IDs.
  8. comment__in (array): Comment IDs to include in the query.
  9. comment__not_in (array): Comment IDs to exclude from the query.
  10. karma (integer): The “karma” score to return matching comments for. (Remember comment_karma from the previous section?)
  11. number (integer): The maximum number of comments to return.
  12. offset (integer): The number of comments to pass over in the query.
  13. orderby (string or array): A comment status or an array of statuses to order the query results. Accepts all the keys returned from the query() method, plus ‘meta_value’, ‘meta_value_num’, value of $meta_key, FALSE, empty array or ‘none’. (The last three disable the ORDER BY clause in the query.)
  14. order (string): How to order retrieved comments—’ASC’ for ascending or ‘DESC’ for descending. (Default: ‘DESC’)
  15. parent (integer): Parent comment’s ID to retrieve children.
  16. post_id (integer): Post ID to retrieve comments. (Default: 0)
  17. post__in (array): Post IDs to include in the results.
  18. post__not_in (array): Post IDs to exclude from the results.
  19. post_author (integer): Post author’s ID to limit results by.
  20. post_name (string): Post slug to get comments from.
  21. post_parent (integer): Parent post ID to get comments from.
  22. post_type (string): Post type to get comments from.
  23. post_status (string): Post status to get comments from.
  24. status (string): Comment status to limit results by. Accepts ‘hold’, ‘approve’, ‘all’ or a custom comment status. (Default: ‘all’)
  25. 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.
  26. type__in (array): Comment types to include in the query.
  27. type__not_in (array): Comment types to exclude from the query.
  28. user_id (integer): User ID to include comments of a specific user.
  29. search (string): Search terms to get matching comments for.
  30. count (boolean): Return the comment count (TRUE) or an array of comments (FALSE). (Default: FALSE)
  31. meta_key (string): A custom meta key to include matching comments only.
  32. meta_value (string): A custom meta value to include matching comments only.
  33. meta_query (array): An array of WP_Meta_Query clauses (which we’ll see in the next part of this series).
  34. date_query (array): An array of WP_Date_Query clauses (which we’ll see in the next part of this series). (Default: NULL)

Ghi chú: Giá trị mặt định cho các tham số này là null.

CÁC VÍ DỤ

Ví dụ tạo function hiển thị 5 comment mới nhất của website (thường dùng để show đọ hot, đông vui của website :D)
Xem thêm link gốc bài viết này tại  https://scotch.io/tutorials/retrieving-wp_comment_query-in-wordpress

5/5 - (1 bình chọn)
5 2 đánh giá
Đánh giá bài viết
Theo dõi
Thông báo của
guest
0 Góp ý
Phản hồi nội tuyến
Xem tất cả bình luận
0
Rất thích suy nghĩ của bạn, hãy bình luận.x