Last active
August 29, 2015 14:06
-
-
Save birgire/6d8e3dfb3416de5acdbf to your computer and use it in GitHub Desktop.
WordPress: Comments count
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Usage Examples: | |
*/ | |
$post_id = 3522; | |
echo 'Counting all normal comments for a given post: ' . wpq_get_normal_comments_count( $post_id ); | |
echo 'Counting all extra comments for a given post:' . wpq_get_extra_comments_count( $post_id ); | |
echo 'Counting all extra comments, without replies, for a given post:' . wpq_get_extra_comments_without_replies_count( $post_id ); | |
$comment_id = 432; | |
echo 'Counting all 1st level replies to a comment: ' . wpq_get_replies_per_comment_count( $comment_id ); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Count approved replies ( 1 level deep) to a given extra comment: | |
*/ | |
function wpq_get_replies_per_comment_count( $comment_id ) | |
{ | |
global $wpdb; | |
$sql = $wpdb->prepare( | |
"SELECT COUNT(1) AS cc FROM {$wpdb->comments} WHERE comment_approved = 1 AND comment_parent = %d", | |
$comment_id | |
); | |
return $wpdb->get_var( $sql ); | |
} | |
/** | |
* Count approved extra comments without replies | |
*/ | |
function wpq_get_extra_comments_without_replies_count( $post_id ) | |
{ | |
global $wpdb; | |
$sql = $wpdb->prepare( | |
"SELECT COUNT(1) AS cc FROM {$wpdb->comments} WHERE comment_type = 'extra' AND comment_post_ID = %d AND comment_approved = 1 AND comment_parent = 0", | |
$post_id | |
); | |
return $wpdb->get_var( $sql ); | |
} | |
/** | |
* Count approved normal comments | |
*/ | |
function wpq_get_normal_comments_count( $post_id ) | |
{ | |
global $wpdb; | |
$sql = $wpdb->prepare( | |
"SELECT COUNT(1) AS cc FROM {$wpdb->comments} WHERE comment_type IN ( '', 'pingback', 'trackback') AND comment_post_ID = %d AND comment_approved = 1 ", | |
$post_id | |
); | |
return $wpdb->get_var( $sql ); | |
} | |
/** | |
* Count approved extra comments | |
*/ | |
function wpq_get_extra_comments_count( $post_id ) | |
{ | |
global $wpdb; | |
$sql = $wpdb->prepare( | |
"SELECT COUNT(1) AS cc FROM {$wpdb->comments} WHERE comment_type IN ( 'extra' ) AND comment_post_ID = %d AND comment_approved = 1 ", | |
$post_id | |
); | |
return $wpdb->get_var( $sql ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment