Helper function
Here’s a helper function (untested) to check if a given user ID is the first commenter for a given post ID:
/**
* Check if a given user ID is the first commenter for a given post ID
*
* @param int $user_id User ID
* @param int $post_id Post ID
* @return bool True/False
*/
function wpse_is_user_first_commenter( $user_id = 0, $post_id = 0 )
{
$first_comment = get_comments(
[
'status' => 'approve',
'number' => 1,
'order' => 'ASC',
'orderby' => 'comment_date_gmt',
'post_id' => (int) $post_id,
]
);
if( empty( $first_comment ) )
return false;
return $first_comment[0]->user_id === $user_id;
}
Usage example:
Check if the current user is the first commenter of the current post:
if ( wpse_is_user_first_commenter( get_current_user_id(), get_the_ID() ) )
{
// ...
}
Hope you can adjust it to your needs!
Related Posts:
- Order of Operation for these three hooks
- How to check if `comment_meta` exists before inserting the comment?
- Plugin Form Submission Best Practice
- Plugin directory “Last Updated” not changed after initial commit?
- What is the recommended way to create plugin administration forms?
- Should I create a theme or a plugin?
- Fatal error: Call to undefined function wp_mail()
- Does WordPress have an Browser Agent?
- How Do I Load My Action Earlier Enough?
- How to check WordPress website username and password is correct
- How to sync with plugins update after I have done manual optimizations?
- How to retrieve a value via get_option when the option_value is stored in a multi-dimensional array?
- Filter on the_content ignores shortcodes
- Custom theme sufficient or custom plugin neccessary for this feature set?
- Check spam in custom form – akismet
- Add Admin Menu Inside Construct or Init
- get plugin directory url
- WordPress cron running twice
- How to create thumbnail of different files?
- PHP library that can merge stylesheet with inline style [closed]
- Admin-ajax.php appending a status code to ajax response
- comment_email hook doesn’t seem to work for comment editor field
- How can I add a field to make link nofollow to the WordPress Link Inspector Window?
- Version upgrade: can my plugin filter the SQL statements issued during a database upgrade?
- admin_notices not displaying in plugin
- How to get color name in PanelColorSettings in custom Gutenberg block?
- Get Objects While Deleting term
- Cannot redeclare function error on the same line
- js/css updating when making a plugin
- settings_fields() doesn’t work for other roles except admin
- Subscribe to author?
- how to make custom bulk actions for a WP_List_Table (core class)
- Get name and email of current unregistered user, who has recently submitted name and email in comment form?
- Append country to ‘pretty’ url but serve same page
- Set page template programaticlly in plugin
- Adding a brand column to WooCommerce Products
- How to use output buffering in WordPress hooked functions?
- Plugin menu addition in multisite
- Show custom data instead of 404
- Mixing and Matching – Custom templates in a WordPress plugin
- What’s the right way to instantiate new blocks and/or the plugin itself in toto when using wppb architecture?
- Open Graph Object Debugger & url parameters – Page Not Found result
- How to get Metabox custom field to show checked if value is updated using post meta query?
- wp_schedule_event seems to be being added twice
- How to Move the Comments Bubble to the Right Side of the Toolbar
- How should I go about registering JavaScript that isn’t a file? [duplicate]
- What happens when two or more plugins use the same hook?
- Can’t find where to modify attrbitutes
- How to pull user/author profile data in a plugin?
- Adding columns to core tables
- Add Password Generator on password protected page
- WordPress pages are not published due to External database connection with WPDB class
- How to optimize multiple insert into wordpress database
- Injecting HTML on (arbitrary) admin pages
- Custom Stylesheet instead of Inline CSS for WordPress Customizer API Overrides
- How to add custom options for plugin into the add post screen – like yoast seo
- How to get terms for taxonomy
- If $var is empty, return 404.
- Listing wordpress users with a search function
- Cannot stop wpautop from messing up my plug-in output
- Plugin won’t activate
- dbdelta failing with error: “WordPress database error Table ‘wp_2_myPlugin’ already exists
- Plugin could not be activated because it triggered a fatal error?
- Get cat parameter from admin-ajax
- Update product prices from remote file
- How to determine primary editor for a page/post
- Using wp_mail to send email?
- Which wp plugin can use Create, Edit and Delete Email address from WordPress Dashboard
- product-attribute-slug-is-too-long-28-characters-max
- Custom plugin & calling REST api of wordpress instance the plugins is running on
- Limit get_next_post to posts from the same author
- Show list of categories that has posts with different taxonomies
- “add to cart” links css class “ajax_add_to_cart” doesn’t show in woocommerce in widget sidebar
- how to create a shortcode from a variable in plugin
- Any way, hook to add content right before the “read more” link?
- How do I specify where to save the debug.log?
- Error Connecting to Database WHEN Installing WordPress on XAMPP [Tried All the Usual Stuff] (Pics Included)
- How do I ensure I can loop through every enqueued script and CSS?
- What is the source of the $post_id in a hook argument?
- WordPress Fatal error: Uncaught Error: Call to undefined function dbDelta()
- Adding body class in author page for custom role
- Plugins Settings page not updating
- TinyMCE 4.7.11 – Enable hidden WordPress core plugins? (referencing a wp-includes from plugin.php)
- Enqueue scripts based on site.
- My enqueue admin style function doesn’t work because of ?=ver
- Use content filter on the post that is password-protected
- WordPress filter custom posts by multi terms of of a taxonomy
- foreach argument to get specific file types getting too many returns
- How to fetch the post content from the wordpress editor before it can be saved(published)?
- How does one set cookies in WordPress without getting the ‘headers not sent’ error? within WP
- How can I give access to my plugin sections in admin?
- Filter the_content() to add custom figure tags
- Making plugin output customizable
- Return all roles, including custom roles
- Mapping multiple URLs to same page
- Installing plugins on installation/activation
- Add query string variables to all hyperlinks URL matching a specified domain
- Loading jQuery library from WordPress admin
- Advanced Custom Fields conflicting with custom plugin when saving custom field
- WordPress plugin boilerplate AJAX functionality