[wp-trac] [WordPress Trac] #37255: wp_get_attachment_caption and post parameter

WordPress Trac noreply at wordpress.org
Sat Jul 2 09:26:04 UTC 2016

#37255: wp_get_attachment_caption and post parameter
 Reporter:  wido               |      Owner:
     Type:  enhancement        |     Status:  new
 Priority:  normal             |  Milestone:  Awaiting Review
Component:  Posts, Post Types  |    Version:  trunk
 Severity:  normal             |   Keywords:
  Focuses:                     |
 Some WordPress functions accept the $post object as parameter instead only
 the $post_id.

 Since most of the time we write our code within the loop or we use the
 $post object, could be an improvement to standardize the functions that
 require a post id even support the $post object?

 For the wp_get_attachment_caption for example, could be like this:

  * Retrieves the caption for an attachment.
  * @since 4.6.0
  * @param int|WP_Post|null $post Optional. Post ID or post object.
 Defaults to global $post.
  * @return string|false False on failure. Attachment caption on success.
 function wp_get_attachment_caption( $post = null ) {

         if ( ! $post instanceof WP_Post ) {
                 if ( ! $post = get_post( $post ) ) {
                         return false;

         if ( 'attachment' !== $post->post_type ) {
                 return false;

         $caption = $post->post_excerpt;

          * Filters the attachment caption.
          * @since 4.6.0
          * @param string $caption Caption for the given attachment.
          * @param WP_Post   $post Attachment object.
         return apply_filters( 'wp_get_attachment_caption', $caption, $post

 The get_the_post_thumbnail_caption that use the wp_get_attachment_caption
 accept the $post object.

 Also, instead of passing the $post->ID to the wp_get_attachment_caption,
 use the $post object, so we can work directly with the object instead of
 calling again the get_post function to retrieve it.

Ticket URL: <https://core.trac.wordpress.org/ticket/37255>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform

More information about the wp-trac mailing list