WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#14294 closed defect (bug) (duplicate)

get_permalink() calling get_post_permalink() with post object instead of post ID

Reported by: mikeschinkel Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

Can someone please check my logic on this? It appears that get_permalink() in WP 3.0 (/wp-includes/link-template.php line 111) is calling get_post_permalink() with a post object instead of post ID as the first parameter.

The signature for get_post_permalink() is:

function get_post_permalink( $id = 0, $leavename = false, $sample = false )

And then it applies the "post_type_link" filter on line 207 without touching $id hence without attempting to change it's data type:

return apply_filters('post_type_link', $post_link, $id, $leavename, $sample)

I found it because it was breaking my use of the "post_type_link" hook on code that previously worked in a beta version of 3.0. The fix is easy, just change

return get_post_permalink($post, $leavename, $sample);

to

return get_post_permalink($post->ID, $leavename, $sample);

I was able to resolve it in my own "post_type_link" hook with this:

$id = (is_numeric($id) ? $id : is_object($id) and isset($id->ID) ? $id->ID : 0);

But wouldn't it be better just to fix the bug, assuming it is a bug?

OTOH, please let me know if this is my misunderstanding and what I should be doing differently; it probably is because this seems too easy.

Change History (8)

comment:1 @mikeschinkel5 years ago

Egads! Ghosts of programming languages past. My "in hook" fix was supposed to be:

$id = (is_numeric($id) ? $id : is_object($id) && isset($id->ID) ? $id->ID : 0); 

Sorry about that "and" oversight.

comment:2 @nacin5 years ago

#14299

Makes sense we should ensure the filter always returns an ID, I think. Or maybe always a post object. Not sure which is more expected.

comment:3 follow-up: @filosofo5 years ago

As I mentioned over there, post_link filter has post object as second argument, so it makes sense to be consistent with that and pass the post object to the second argument of the callback on post_type_link.

comment:4 in reply to: ↑ 3 @mikeschinkel5 years ago

Replying to filosofo:

As I mentioned over there, post_link filter has post object as second argument, so it makes sense to be consistent with that and pass the post object to the second argument of the callback on post_type_link.

Whichever, something needs to change, right?

comment:5 follow-up: @filosofo5 years ago

It might make sense to keep the stated argument type as an integer, as I suggest in my second patch on #14299, but keep it an object passed to the filter. That would make it more parallel to get_permalink.

It's curious that we discovered this issue independently within a day of each other.

comment:6 in reply to: ↑ 5 @mikeschinkel5 years ago

Replying to filosofo:

It's curious that we discovered this issue independently within a day of each other.

Validates the "many eyes" theory of open source...

comment:7 @nacin5 years ago

  • Resolution set to duplicate
  • Status changed from new to closed

Mike -- indeed.

Closing as a duplicate; fixed in #14299.

comment:8 @scribu5 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.