#40096 closed enhancement (fixed)
get_post_thumbnail_id should return int
Reported by: | 0v3rth3d4wn | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 5.4 | Priority: | normal |
Severity: | normal | Version: | 2.9 |
Component: | Post Thumbnails | Keywords: | |
Focuses: | docs | Cc: |
Description
get_post_thumbnail_id returns string, which sometimes irritates me. I know that it actually returns get_post_meta, which returns string, but why don't cast it to int or wrap it in absint.
<?php function get_post_thumbnail_id( $post = null ) { $post = get_post( $post ); if ( ! $post ) { return ''; } return absint( get_post_meta( $post->ID, '_thumbnail_id', true ) ); }
Change History (8)
#2
@
5 years ago
- Component changed from General to Post Thumbnails
- Focuses docs added
- Version changed from 4.7.3 to 2.9
From #45658, the inline documentation says `@return string|int, but an integer is not returned.
#3
@
5 years ago
- Milestone changed from Awaiting Review to 5.4
- Owner set to SergeyBiryukov
- Status changed from new to reviewing
When changing the return type, we have to be careful not to introduce back compat issue like in #22324.
That said, I think it makes sense to clean this up while still early in the cycle. It would make the function more consistent with get_the_ID()
or wp_get_post_parent_id()
, both returning an integer.
Note that some unit tests already cast the result to (int)
:
Version 0, edited 5 years ago
by
(next)
This ticket was mentioned in Slack in #core by themiked. View the logs.
5 years ago
Note: See
TracTickets for help on using
tickets.
#45658 was marked as a duplicate.