WordPress.org

Make WordPress Core

Opened 8 months ago

Last modified 2 months ago

#25196 new enhancement

add new function get_comment_post_ID()

Reported by: alexvorn2 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Comments Keywords:
Focuses: Cc:

Description

add a new function that will return the id of the post of the comment

We have get_comment_ID() but no get_comment_post_ID();

the function can be like this:

function get_comment_post_ID() {
	global $comment;
	return apply_filters('get_comment_post_ID', $comment->comment_post_ID);
}

Change History (6)

comment:1 follow-up: nacin8 months ago

We should remove the by-reference for get_comment() and allow it to return the global, like get_post(). Then it becomes get_comment()->comment_post_ID.

comment:2 in reply to: ↑ 1 ; follow-up: nofearinc8 months ago

Replying to nacin:

We should remove the by-reference for get_comment() and allow it to return the global, like get_post(). Then it becomes get_comment()->comment_post_ID.

I think that a function would be more elegant, but if we need it for a "custom" ID (randomly passed) then get_comment() would return null and calling a property of a null would result to a notice?

comment:3 alex-ye8 months ago

  • Cc nashwan.doaqan@… added

comment:4 in reply to: ↑ 2 ; follow-up: nacin8 months ago

Replying to nofearinc:

I think that a function would be more elegant, but if we need it for a "custom" ID (randomly passed) then get_comment() would return null and calling a property of a null would result to a notice?

But if you're trying to fetch the post ID of a comment that doesn't exist, surely you should get a notice?

From another angle: What should be the return value of get_comment_post_ID() when the comment doesn't exist?

I'm just not sure we need to add wrapper functions for every property of a post/comment/etc object. Now, in this case, it could qualify as a template tag — but is it one we honestly need?

comment:5 in reply to: ↑ 4 nofearinc8 months ago

I would definitely go for checking against null or a WP_Error instance rather than getting a notice which is not interceptable (for both functions).

For example, an serialized array of comment IDs is stored by a random plugin in postmeta for a post (for stats purposes) and you want to read them later. Then it's not going to be handled properly?

Last edited 8 months ago by nofearinc (previous) (diff)

comment:6 mordauk2 months ago

If introduced get_comment_post_ID() should allow a comment ID to be passed. If an ID is passed, it'd just call get_comment() to retrieve the comment object.

If no comment ID is passed it could fall back to the $comment global.

Note: See TracTickets for help on using tickets.