[wp-trac] [WordPress Trac] #37255: Update attachment functions to accept a post object in addition to ID

WordPress Trac noreply at wordpress.org
Wed Mar 15 21:14:51 UTC 2023


#37255: Update attachment functions to accept a post object in addition to ID
-------------------------------------------------+-------------------------
 Reporter:  wido                                 |       Owner:
                                                 |  johnbillion
     Type:  enhancement                          |      Status:  accepted
 Priority:  normal                               |   Milestone:  Future
                                                 |  Release
Component:  Media                                |     Version:  4.6
 Severity:  normal                               |  Resolution:
 Keywords:  needs-testing needs-dev-note needs-  |     Focuses:
  testing-info 2nd-opinion                       |
-------------------------------------------------+-------------------------

Comment (by azaozz):

 Replying to [comment:48 Howdy_McGee]:
 > Both `image_downsize()` and `image_get_intermediate_size` eventually
 call the `get_post()`

 Yes, but that's not an excuse to accept inconsistent parameters in
 functions that work on images. That would be exactly the opposite of the
 reason this could make sense:

  Perhaps may be nice to standardize functions that do something with WP
 posts to accept either $post->ID or $post, purely for consistency. It
 doesn't make any difference "under the hood", just affects "code
 readability and self-documenting" a little. That may be a bit "weird" in
 some places, but being consistent even when weird is not that bad, maybe
 :)

 > If passed an object we can skip `WP_Post::get_instsance( $post_id )`
 which checks the cache, and makes a possible database call to grab the
 object.

 Yes, exactly. `WP_Post` is cached and `get_post()` uses that cache. It is
 the proper way to get the `WP_Post` instance, and retrieving it from the
 cache is extremely fast. It returns the "proper" instance after filters
 and actions have run, etc.

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


More information about the wp-trac mailing list