On my custom dashboard, I want to show the number of total new comments (somehow like “right now”). Just like “You have … open comments to take care of”. So all I want is the total number not a list for each post or so.
But I can’t get this right. Any help out there?
In the meantime I got some info (from wpmudev) on this, telling me that this is heavy stuff and not a very basic question:
This is less basic than you might think.
You need to:
- Register a new meta box to the WP admin
- Query the database for the total new comments since the currently logged in user’s last visit
- Set the visit timeout (so changing page doesn’t just set them to 0 right away, that would be frustrating)
- Write the CSS to make the display pretty
- You’ll find hints as to what to do if you examine the WordPress core in the “Right Now” widget but that’s probably going to be over your head.
At that point, you may as well publish it as a plugin. It’s possible and as far as plugins go, it’s barely intermediate, but it’s still very advanced for this forum.*
UPDATE
This is what finally made me happy, thanks again to @toscho. It is showing the number of comments, that are waiting for moderation. It can be placed everywhere you want it to show up.
<?php
function t5_count_new_comments()
{
global $wpdb;
// last user access
$last_access = get_user_meta( get_current_user_id(), 'last_access', TRUE );
// comment query
$where = $wpdb->prepare( "WHERE comment_date > %s AND comment_approved='0'", $last_access );
$comment_query = $wpdb->get_results(
"SELECT comment_ID,
COUNT( comment_ID ) AS new_comments
FROM {$wpdb->comments} $where",
OBJECT
);
if ( ! isset ( $comment_query[0]->new_comments ) )
return 0;
return $comment_query[0]->new_comments;
}
$new_comments = t5_count_new_comments();
echo "There are $new_comments new comments.";
?>
Actually, it is not that hard.
get_user_meta( get_current_user_id(), 'last_access', TRUE )
.comment_date
.>
.right_now_discussion_table_end
. See the filewp-admin/includes/dashboard.php
.Now letâs stick it together:
Result:
Download from GitHub
In response to your comments: To get just the number of new comments a an integer, use something like this:
Now you can use that function in your custom code: