Home » Classes » WP_Comment_Query

One of the most important parts of any website is the ability for visitors to leave comments and interact with the content. In WordPress, comments are stored in the database and can be queried and displayed in a variety of ways using the WP_Comment_Query class.

The WP_Comment_Query class is used to retrieve comments from the database based on specified parameters. These parameters can include things like the post ID, comment status, author email, and more. This class provides methods for setting these parameters, executing the query, and returning the results.

For example, if you wanted to retrieve all comments for a specific post, you could use the following code:

$comment_query = new WP_Comment_Query( array(
    'post_id' => 123
) );

$comments = $comment_query->get_comments();

In this example, we create a new WP_Comment_Query object and pass in an array of parameters. We set the post_id parameter to 123, indicating that we want to retrieve comments for the post with ID 123. We then call the get_comments() method to execute the query and return the results.

Overall, the WP_Comment_Query class is a powerful tool for retrieving and displaying comments on your WordPress website. Whether you’re building a custom comments widget or just want more control over how comments are displayed on your site, this class is worth exploring.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.