#40096 closed enhancement (fixed)
get_post_thumbnail_id should return int
| Reported by: | 0v3rth3d4wn | Owned by: | SergeyBiryukov |
|---|---|---|---|
| Priority: | normal | Milestone: | 5.4 |
| Component: | Post Thumbnails | Version: | 2.9 |
| Severity: | normal | Keywords: | |
| Cc: | Focuses: | docs |
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
@
7 years ago
- Component General → Post Thumbnails
- Focuses docs added
- Version 4.7.3 → 2.9
From #45658, the inline documentation says `@return string|int, but an integer is not returned.
#3
@
7 years ago
- Milestone Awaiting Review → 5.4
- Owner set to
- Status new → reviewing
When changing the return type, we have to be careful not to introduce back compat issues 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):
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
#45658 was marked as a duplicate.