#14294 closed defect (bug) (duplicate)
get_permalink() calling get_post_permalink() with post object instead of post ID
| Reported by: |
|
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)
#2
@
15 years ago
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.
#3
follow-up:
↓ 4
@
15 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.
#4
in reply to:
↑ 3
@
15 years ago
Replying to filosofo:
As I mentioned over there,
post_linkfilter 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 onpost_type_link.
Whichever, something needs to change, right?
#5
follow-up:
↓ 6
@
15 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.
#6
in reply to:
↑ 5
@
15 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...
Egads! Ghosts of programming languages past. My "in hook" fix was supposed to be:
Sorry about that "and" oversight.