wp_comments

Stores comments and other comment-like content (pingbacks, trackbacks).

Schema

ColumnTypeNullDefaultDescription
comment_IDbigint(20) unsignedNOauto_incrementUnique identifier
comment_post_IDbigint(20) unsignedNO0Post ID (FK → wp_posts.ID)
comment_authortinytextNONULLAuthor display name
comment_author_emailvarchar(100)NOAuthor email address
comment_author_urlvarchar(200)NOAuthor website URL
comment_author_IPvarchar(100)NOAuthor IP address
comment_datedatetimeNO0000-00-00 00:00:00Comment date (local)
comment_date_gmtdatetimeNO0000-00-00 00:00:00Comment date (GMT)
comment_contenttextNONULLComment text
comment_karmaint(11)NO0Rating/karma score
comment_approvedvarchar(20)NO‘1’Status: 1, 0, spam, trash
comment_agentvarchar(255)NOUser agent string
comment_typevarchar(20)NO‘comment’Type: comment, pingback, trackback
comment_parentbigint(20) unsignedNO0Parent comment ID (for threading)
user_idbigint(20) unsignedNO0User ID if logged in (FK → wp_users.ID)

Indexes

Key NameColumnsUniquePurpose
PRIMARYcomment_IDYesPrimary key
comment_post_IDcomment_post_IDNoComments for a post
comment_approved_date_gmtcomment_approved, comment_date_gmtNoApproved comments sorted by date
comment_date_gmtcomment_date_gmtNoGlobal date ordering
comment_parentcomment_parentNoThread replies
comment_author_emailcomment_author_email(10)NoLookup by email

Foreign Key Relationships

  • comment_post_IDwp_posts.ID
  • comment_parentwp_comments.comment_ID (self-referential)
  • user_idwp_users.ID (optional, 0 for guests)

Referenced by:

  • wp_commentmeta.comment_idwp_comments.comment_ID

Comment Status Values

ValueDescription
1Approved
0Pending moderation
spamMarked as spam
trashIn trash
post-trashedComment’s post is trashed

Comment Types

TypeDescription
commentStandard comment (or empty string)
pingbackAutomated pingback
trackbackManual trackback
Custom typesVia plugins (e.g., reviews)

Common Queries

Get approved comments for a post

sql
SELECT * FROM wp_comments 
WHERE comment_post_ID = 123 
  AND comment_approved = '1'
ORDER BY comment_date ASC;

Get threaded comments

sql
SELECT * FROM wp_comments 
WHERE comment_post_ID = 123 
  AND comment_approved = '1'
ORDER BY comment_parent ASC, comment_date ASC;

Get comment with author info

sql
SELECT c.*, u.display_name as user_display_name
FROM wp_comments c
LEFT JOIN wp_users u ON c.user_id = u.ID
WHERE c.comment_ID = 456;

Get recent comments

sql
SELECT c.*, p.post_title
FROM wp_comments c
JOIN wp_posts p ON c.comment_post_ID = p.ID
WHERE c.comment_approved = '1'
  AND c.comment_type = 'comment'
ORDER BY c.comment_date_gmt DESC
LIMIT 10;

Count comments by status

sql
SELECT 
  comment_approved,
  COUNT(*) as count
FROM wp_comments
GROUP BY comment_approved;

Get comments from specific user

sql
SELECT c.*, p.post_title
FROM wp_comments c
JOIN wp_posts p ON c.comment_post_ID = p.ID
WHERE c.user_id = 1
ORDER BY c.comment_date DESC;

Find spam patterns

sql
SELECT comment_author_email, COUNT(*) as count
FROM wp_comments
WHERE comment_approved = 'spam'
GROUP BY comment_author_email
ORDER BY count DESC
LIMIT 20;

Delete old spam

sql
DELETE FROM wp_comments 
WHERE comment_approved = 'spam' 
  AND comment_date < DATE_SUB(NOW(), INTERVAL 30 DAY);

WordPress API Functions

  • get_comment( $comment_id ) – Get single comment
  • get_comments( $args ) – Get comments matching criteria
  • wp_insert_comment( $commentdata ) – Insert comment
  • wp_update_comment( $commentarr ) – Update comment
  • wp_delete_comment( $comment_id ) – Delete comment
  • wp_set_comment_status( $comment_id, $status ) – Change status
  • get_comment_count( $post_id ) – Count comments for post

Privacy/GDPR Notes

Comments store PII:

  • IP addresses (comment_author_IP)
  • Email addresses (comment_author_email)
  • User agents (comment_agent)

Consider:

  • Anonymizing old comments
  • Providing data export/deletion for commenters
  • Cookie consent for comment forms