The pending release of WordPress 3.2 promises to add exciting new features to the already amazing WordPress platform, such as a refreshed admin user interface, a new default theme, full screen editor mode, and significant speed improvements. As is often the case with most WordPress releases, the big enhancements overshadow the small improvements. These small improvements are not usually talked about, but are ones that make the work of developers much easier. One such improvement in WordPress 3.2 is the change to the
Until WordPress 3.2, the
get_comments functions could only accept the following 15 parameters in an array as its argument:
Indeed, these parameters allowed a developer to perform powerful queries to get specific comments for use in themes or plugins; however, the release of WordPress 3.2 brings 5 additional parameters that allow for access to more comments, and comments that match more specific criteria. These new parameters are:
The WordPress 3.0 release brought the revolutionary new custom post type feature; however, until the release of WordPress 3.2, there was not an easy way to get all of the comments associated with a specific custom post type. The new
get_comments parameter ‘post_type’ allows for such a query. Prior to WordPress 3.2, one could access all of the comments associated with a custom post type with a custom query like the following:
1 2 3 4 5 6 7 8 9 10
global $wpdb; $sql = "SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' AND post_type = 'your_custom_post_type' ORDER BY comment_date_gmt DESC LIMIT 5"; $comments = $wpdb->get_results($sql);
Where possible, I always like to use the built in WordPress functions when interacting with the database in order to maintain forward compatibility in the event that the WordPress database schema changes. The comments for a custom post type can definitely be retrieved in this manner, but with the improvements to
get_comments in WordPress 3.2, this same task can be accomplished with a WordPress function in a much simpler manner:
1 2 3 4 5
$comments = get_comments( array( 'post_type' => 'your_custom_post_type' ) );
Simpler, cleaner, forward compatible, and a little easier on the eyes! One case example might be to separate out comments in a widget by type. For instance, if you had a custom post type of ‘review’, you could display your regular blog comments separated out from your review comments in a sidebar widget.
The WordPress development team always sneaks in amazing improvements in each version of the platform. With enhancements such as the ones to the
get_comments function, they certainly have not rested on their laurels!