Make WordPress Core

Opened 9 years ago

Closed 7 years ago

Last modified 6 years ago

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

#1 @desrosj
7 years ago

#45658 was marked as a duplicate.

#2 @desrosj
7 years ago

  • Component GeneralPost Thumbnails
  • Focuses docs added
  • Version 4.7.32.9

From #45658, the inline documentation says `@return string|int, but an integer is not returned.

#3 @SergeyBiryukov
7 years ago

  • Milestone Awaiting Review5.4
  • Owner set to SergeyBiryukov
  • Status newreviewing

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 years ago by SergeyBiryukov (previous) (diff)

#4 @SergeyBiryukov
7 years ago

  • Resolutionfixed
  • Status reviewingclosed

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
7 years ago

In 47162:

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

See #40096.

#6 @SergeyBiryukov
7 years ago

In 47163:

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

Follow-up to [47162].

See #40096.

This ticket was mentioned in Slack in #core by themiked. View the logs.


6 years ago

#8 @SergeyBiryukov
6 years ago

In 48310:

Post Thumbnails: Change the return value of get_post_thumbnail_id() for a non-existing post to false instead of an empty string.

This further makes the function more consistent with get_the_ID() or wp_get_post_parent_id(), both returning false for a non-existing post.

Additionally, document that get_post_thumbnail_id() returns 0 if the thumbnail image is not set.

Follow-up to [47160].

Props theMikeD, dingo_d, netpassprodsr, SergeyBiryukov.
Fixes #49832. See #40096.

Note: See TracTickets for help on using tickets.