Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Get Post ID within wp_set_comment_status hook

You should consider removing the average_ratingfn() function definition out of there and call it with an input argument for the post ID.

You can try to fetch the comment object with (untested):

$comment = get_comment( $comment_ID );

and then make sure it’s not null or you have an actual comment object with:

if( $comment instanceof \WP_Comment )
    $post_id = $comment->comment_post_ID;

You should be able to get the comment_post_ID field from that object.

I think you could also use the hooks within the wp_transition_comment_status() function, where the comment object is among the input arguments.

For example the hooks:

  • comment_{$new_status}_{$comment->comment_type}

  • comment_{$old_status}_to_{$new_status}

  • transition_comment_status

Related Posts:

  1. WordPress for questions and responses website
  2. Add comment_meta to wp_comment_reply
  3. How do I comment out a block of tags in XML?
  4. R: Comment out block of code
  5. Why do I get comment spam even with Akismet and Captcha?
  6. What tools are available for managing/writing to WordPress? [closed]
  7. How to rearrange fields in comment_form()
  8. setting comments off as default for pages and custom post types?
  9. Is it possible to pull comments from facebook into your blog?
  10. Filtering the Admin Comments List to Show Only Comments from the Current User?
  11. Non-threaded comment replies with link to original comment
  12. Approve comment hook?
  13. Commenting in user profile page?
  14. How to change “You must be logged in to post a comment.”
  15. comment_post_ID 0 (cannot remove from dashboard)
  16. Disable comments on all posts/pages
  17. How do I delete all comments from a specific old blog post?
  18. Removing the “Website” Field from Comments and Replies?
  19. Stop WordPress redirecting comment-page-1 to the post page?
  20. Importing old Disqus comments into WordPress
  21. How to add a class to the comment submit button?
  22. How to wrap submit button of comment form with div
  23. How to enable comments for pending and draft posts?
  24. Using WordPress’ WYSIWYG for comments
  25. Enable Submit Comment Without Page Reload (Using Ajax)?
  26. What for is the table “wp_commentmeta” exactly?
  27. Getting Post Comments for post ID using WP_Query() and a Custom Loop?
  28. Add option to disable comments on a per posts basis?
  29. Resetting comment count
  30. When importing – failed to import: Invalid post type feedback
  31. How to change the email notification recipient (user) for new comments?
  32. Redirect user to a custom url after submitting the comment
  33. Paginate result set from $wpdb->get_results()
  34. Change Comment Author Display Name
  35. Would switching to InnoDB from MyISAM improve performance of comments table?
  36. Custom comment type based on thread level
  37. How to add internal, revision comments to page updates
  38. How to load and show comments with AJAX instead of pagination?
  39. Linking to Page Showing Only Comments Without Parent Post
  40. Comment Reply javascript
  41. How do we remove the H3 tag for the reply-title I.D
  42. Comments not appearing at all
  43. comments reply script not working
  44. How to display comment form error messages in the same page
  45. 3 moderators to approve comment
  46. How to deal with small scale comment spam on small commercial sites? [closed]
  47. What should I do to make generated avatars different for anonymous comments?
  48. A plugin where users can comment with Facebook or Twitter or OpenID [closed]
  49. Check If comment author is registered
  50. Comments screen in backend, how to disable email address of commenter for non admins
  51. Add comments from the admin panel?
  52. How can I limit the number of comments per registered user per day?
  53. One comment per user per post but be able to reply to existing comments
  54. How to use a custom comments template
  55. Comment visibility
  56. What’s the easiest way to close comments on media/attachments?
  57. Reverse comment pagination numbers
  58. Get comments for more than one post
  59. How can I add comments to a page?
  60. How to remove comment spam in WordPress
  61. Post Comments using WP REST API v2 in WordPress
  62. show number of open comments on custom dashboard
  63. Show content only if member left a comment
  64. Add placeholder attribute to comment form fields
  65. Why do I get accidental comments without (the required) email address?
  66. How is comment spam received without a comments form?
  67. Does a reply to a wordpress comment notify the author of the comment?
  68. What are the current recommended best-practices for comments.php?
  69. human_time_diff() returns “48 years ago” for all comments
  70. How do I set up real anonymous posting in bbpress forums? [closed]
  71. Comment Count for each Comment Author
  72. Link name in comments to Author page? Comment Author Meta in Comments?
  73. How do I turn off wordpress comments ability to capture a users ip address?
  74. Showing comments only to same custom user role
  75. Parent comment’s author name
  76. Success message in comment form
  77. get_comments_number of depth-1 (Level 1) (1 post)
  78. Show comments from multiple post IDs in comment template
  79. Running a function on comment status change
  80. How to allow the reply link to remain on the comment form after I have reached my 10 nested comment limit?
  81. How can I control the comment counts filtering my CPT replies?
  82. Hook to edit an column on comments screen?
  83. Allowing more elements in comments via functions.php
  84. Set post comments open function
  85. Comment Author Name In Reply Form
  86. How to auto-approve internal pingbacks?
  87. Deep customization of the comment form?
  88. Post comment as different user than logged in
  89. “Categories” for comments?
  90. get recent comments of a particular category
  91. Threaded comments – deleting parent comment leads to orphan comments
  92. How to hide “Trackback” on paginated comments
  93. Allow anonymous comments, but prevent spam [closed]
  94. Where to remove from comment’s feed?
  95. Hook/Filter before and after comments
  96. Using filter to add additional fields to comment_form()
  97. How to add editor-style.css styling to wp_editor on front end for comments
  98. How to remove commenters ability to add hyperlinks to comments?
  99. Comment form validation
  100. Why are default comments deprecated?
Categories comments Tags comments, post-meta
Show related posts that match two categories
WordPress query with items from more than one selfdefined taxonomy as `term` argument

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress