Make WordPress Core

Opened 8 years ago

Closed 5 years ago

Last modified 4 years ago

#40096 closed enhancement (fixed)

get_post_thumbnail_id should return int

Reported by: 0v3rth3d4wn's profile 0v3rth3d4wn Owned by: sergeybiryukov's profile 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)

#1 @desrosj
5 years ago

#45658 was marked as a duplicate.

#2 @desrosj
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 @SergeyBiryukov
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 SergeyBiryukov (next)

#4 @SergeyBiryukov
5 years 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
5 years ago

In 47162:

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

See #40096.

#6 @SergeyBiryukov
5 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.


5 years ago

#8 @SergeyBiryukov
4 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.