WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 weeks ago

Last modified 3 weeks ago

#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 (6)

#1 @desrosj
7 weeks ago

#45658 was marked as a duplicate.

#2 @desrosj
7 weeks 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 @SergeyBiryukov
7 weeks 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 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):

Last edited 7 weeks ago by SergeyBiryukov (previous) (diff)

#4 @SergeyBiryukov
3 weeks ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 47160:

Post Thumbnails: Make sure get_post_thumbnail_id() returns an integer, to match the documented return value.

This makes the function more consistent with get_the_ID() or wp_get_post_parent_id(), both returning an integer.

Props 0v3rth3d4wn.
Fixes #40096.

#5 @SergeyBiryukov
3 weeks ago

In 47162:

Tests: Adjust XML-RPC unit tests for [47160].

See #40096.

#6 @SergeyBiryukov
3 weeks ago

In 47163:

Tests: Adjust XML-RPC unit tests for [47160].

Follow-up to [47162].

See #40096.

Note: See TracTickets for help on using tickets.