WP_Comment_Query – WordPress Snippet Generator

Use this WordPress code snippet generator to build complex argument arrays for the WP_Comment_Query class.

  • General
  • Comments
  • Post
  • Search
  • User
  • Pagination
  • Order

General

Item
Input

Comments

Item
Input





Post

Item
Input

Search

Item
Input

User

Item
Input

Pagination

Item
Input

Order & Orderby

Item
Input















Custom Comments Query

      
// WP_User_Query arguments
${{themeSlug || 'your_theme_slug'}}_{{queryVariable || 'your_query_variable'}}_args = array(
        'id' => '{{commentID}}',
        'parent' => '{{commentParentID}}',
        'status' => '{{status}}',
        'karma' => '{{commentKarma}}',
        'type' => '{{type}}',
        'post_id' => '{{postID}}',
        'post_parent' => '{{postParentId}}',
        'post_name' => '{{postName}}',
        'post_author' => '{{postAuthor}}',
        'post_status' => '{{postStatus}}',
        'post_type' => '{{postType}}',
        'search' => '{{searchKeyword}}',
        'user_id' => '{{userID}}',
        'author_email' => '{{authorEmail}}',
        'number' => '{{maximumNumber}}',
        'offset' => '{{offsetNumber}}',
        'order' => '{{order}}',
        'orderby' => '{{orderby}}',
);

// Create the WP_User_Query object
${{themeSlug || 'your_theme_slug'}}_{{queryVariable || 'your_query_variable'}} = new WP_Comment_Query( ${{themeSlug || 'your_theme_slug'}}_{{queryVariable || 'your_query_variable'}}_args );

// Comments Loop
if ( ${{themeSlug || 'your_theme_slug'}}_{{queryVariable || 'your_query_variable'}} ) {
	foreach ( ${{themeSlug || 'your_theme_slug'}}_{{queryVariable || 'your_query_variable'}} as $comment ) {
		// do comments
	}
} else {
	// no comments
}


    

The WP_Comment_Query class allows developers to query the WordPress database tables wp_comments and wp_commentmeta. In simple English, it allows the theme’s or plugin’s developer to retrieve post comments through a GUI.

For this reason, we’ve put together this code snippet generator tool that allows developers and first-time theme authors to integrate comment querying functionality into their WordPress themes. But before we begin, let’s take a closer look at what WP_Comment_Query is and how you can use it.

What Is WP_Comment_Query and How Can I Use It?

The WP_Comment_Query class is designed to take all the heavy lifting out of querying comments in WordPress. Unlike before, we don’t have to query the WordPress database tables directly – we can simply use modular and readable parameters and arguments to do it for us.

WP_Comment_Query is a class, defined in wp-includes/class-wp-comment-query.php, that allows querying WordPress database tables ‘wp_comments‘ and ‘wp_commentmeta‘. The class was introduced in Version 3.1. – WordPress Codex

WP_Comment_Query enables developers to build pages that allow users to browse the site’s comments based on the date they were published, their popularity, and who wrote them. Now that you have a fair idea of how you can use WP_Comment_Query, let’s take a look at how you can get started using this code snippet generator tool.

How Do I Use This WP_Comment_Query Snippet Generator Tool?

This WP_Comment_Query code snippet generator tool was designed to help both developers and first-time theme builders query comments for their WordPress themes without having to code them by hand. To use this tool, simply enter the values next to each field based on your preference and the tool will generate a theme-specific code snippet for you.

Let’s take a look at each individual field one at a time:

  • General Tab

    • Theme Slug: Enter your theme’s slug into this field in all lowercase letters, numbers and/or underscores. This value will be used to prefix functions and variables that would otherwise be incompatible with the WordPress core or any installed themes or plugins. Typically, this is the same string as the text domain, with all hyphens replaced by underscores.
    • Unique Query Variable Identifier: Enter a descriptive name for your query variable. It will be used to identify the query variable. The query variable identifier should be a unique name in all lowercase letters, numbers and/or underscores.
    • Include the Loop: Check this box if you’d like to have the code in the loop be processed on each post. By default, this value is set to false.
  • Comments Tab

    • Comment ID: Enter the comment’s unique ID here. This field accepts an int value.
    • Comment Parent ID: Enter the parent comment’s ID here in case of nested comments. 0 value denotes top-level comments.
    • Comment Status: Denotes the comment’s approval level. There are three options to choose from i.e. Hold (for unapproved comments), Approve (for approved comments), and Trash (for trashed comments).
    • Comment Karma: Enter the comment’s karma here.
    • Comment Type: Select the comment’s type. The available options include Comment, Pingback, Trackback, and Pings.
  • Post Tab

    • Post ID: Add the ID of the post or page for which you would like to returns comments from. This value defaults to none.
    • Post Parent ID: The post ID entered in this field will return comments for only the child pages. To return only the top-level entries and excluding all child pages, set this value to 0. This field accepts an int value.
    • Post Name: Enter the post slug here for which you would like to returns comments from. It should be in all lowercase letters, numbers, and/or underscores.
    • Post Author: Enter the post’s author here for which you would like to returns comments from. It should be in all lowercase letters, numbers, and/or underscores.
    • Post Status: Enter a string/array to retrieve comments from a particular post based on its post status. The default value is publish.
    • Post Type: Enter a string/array to retrieve comments from a particular post based on its post types. The default value is post.
  • Search Tab

    • Search Keyword: Enter a string. The code will search for all possible string matches in comments. You can also use the (*) wildcard before and/or after the string. This allows you to match on comments starting with, ending with, or containing the string you enter.
  • User Tab

    • User ID: Enter the comment author’s ID if they are registered with the website. If they aren’t enter 0.
    • Author Email: Enter the comment author’s email in this field.
  • Pagination Tab

    • Max Number: This value denotes the maximum returned number of results. Enter an “int” value in this field.
    • Offset Number: Enter an “int” value in this field to offset the returned results.
  • Order Tab

    • Order: Choose whether to order the orderby parameters results by Ascending or Descending order. The value defaults to Descending.
    • Orderby: Select which parameter you’d like to sort the retrieved comments by. The value defaults to Comment Date GMT.

How Do I Add the Code Snippet to My Website?

The quickest way to add the generated code snippet to your WordPress site is via FTP. Start off by downloading the theme’s functions.php file to your desktop via an FTP client. Next, open it up in a text editor and paste the code from the Custom Comments Query section at the bottom of the file. Hit the Save button and re-upload it to your theme’s directory.

Alternatively, you can add the generated code snippet to your site by pasting it directly into your current theme’s functions.php file. Simply head over to Appearance > Editor from the admin panel, find your active theme’s functions.php file, and insert the code at the bottom of the file.

Conclusion

WP_Comment_Query offers a powerful and flexible way for developers to query comments from the WordPress database tables through a standardized interface. Hopefully, this code snippet generator tool will help you add comments querying functionality in your WordPress themes and save you some time and effort from having to code it by hand.

Do you have any questions about the WP_Comment_Query code snippet generator tool? Let us know in the comments section below!


Written exclusively for

Nimbus Themes Publishing Logo

About the Author

Evan Scoboria is the co-founder, and developer at Shea Media LLC, the team behind Nimbus Themes, this magazine, and a bunch of very happy clients. He co-founded Shea Media with his wife Kendall in 2009. Evan enjoys hunting, fishing, code, cycling, and most of all WordPress!

Read all posts

Leave a Reply

Your email address will not be published. Required fields are marked *