I don’t believe there is more efficient way. Your primary object should be keeping things simple while using WP’s API which is exactly what you’re already doing. It’s true some scenarios could become pretty expensive and that is where you could take advantage of WP Object Cache or even Transients API. A good example of cache usage are the default widgets found in wp-includes/default-widgets.php
Related Posts:
- How To Retrieve An Image Attachment’s Alt Text?
- getting attachement images src and add classes
- Check if post has attachments (not image)
- Validate alt text for attachments?
- Applying automatic link class to images embedded to posts
- get post attachments of a particular size
- Change image link: wp_get_attachment_link
- Making images from single.php pointing to an attachment .php template
- wp_get_attachment_image_src always returns false
- Exact image sizes
- Get the size (size in Kb or MB) of an featured image?
- query attachment images … if no attachments -> get attachments of parent page?
- Edit default image attachment size
- Change code to display image attachment page
- Attachment 0×0 size error [closed]
- Add image attachment to post via coding?
- Removing width and height attributes from wp-get-attachment
- Get post attachment images dimension and use in embed code
- use wp_get_attachment_image() to show attachments
- Adding width and height to wp_get_attachment_image_src
- Get the bare URL for an attachment
- Display attachment resolution size
- How to check if there is image title, caption, description?
- How to remove attachments size attribute [duplicate]
- How to redirect ‘file url’ to ‘post url’ i.e. in attachment.php
- Check what attached image size is used
- How do I get all attachments that are used in the current post, not just the children?
- Clean up images, delete unused image sizes attached to a post type
- Get title of post image is attached to
- Correct attachment size
- Post Gallery list attachments except the one used as post thumbnail
- Get image attachment to a post outside the loop?
- How do I intercept and modify the functionality of wp_get_attachment_image()?
- Unable to get wp_get_attachment_url to show
- Get Attachment Image Class by attachment id
- Possible to add “Even/Odd Classes” to image attachments via wp_get_attachment_image?
- changing the default picture size link url
- Get an image from the media library. Struggling with the code?
- How to view exact image dimensions of a media in Admin view?
- Filter not applied to all image sizes
- wp_get_attachment_image_src calling wrong location
- Get attachment URL from attachment Link in WordPress
- Display setting ‘size’ not available in image settings modal
- Hide Image Container if No Image Attachments
- Incomplete attachment page
- How to retrieve “custom url” from image attachment in wordpress
- Retreive tags from attached post for media on page
- list of attachments: wp_get_attachment_link works, wp_get_attachment_image does not
- get_image_tag filter not working
- Change attachment inside the_content using filters
- Get the first image from post content (eg.: hotlinked images)
- Get all post attachments except featured image
- How can I bulk delete media and attachments using WP-CLI?
- Disable image attachment links
- How to find attachment by it’s name?
- How can I UN-attach media from a post?
- Associate an existing image with a post
- How to remove title attribute from gallery links and images
- Get the attachment URL on single.php
- How to upload image without post ID using the new media uploader?
- How can I get a different image size, if all I have is the link?
- query attachments of parent page if attachments of current page are smaller than …
- Insert custom ID into wp_get_attachment_link
- How to add attachment without uploading?
- How to change ID of an attachment in wp_posts and its related tables correctly?
- Reorder “menu order” of attachments to another predefined order
- Why can I not view image attachment pages?
- How To Grab All Type Of Attachments, But Images?
- How do i add LIGHTBOX to Attachments?
- How to specify width and height in the_content for image attachments
- delete image attachments of a custom post type except featured image
- Pull images from the gallery
- Rewrite all article pictures to link to file
- Delete associated thumbnails when calling wp_delete_attachment
- How to create resized versions of images added with wp_insert_attachment
- How to allow empty title for attachments?
- One picture belonging different galleries?
- Retrieving post meta array (attachment)
- WordPress Attachments with old images
- 500 internal error at wp_generate_attachment_metadata
- How can I return multiple image attachments with wp_get_attachment_image_src
- How can i catch (with get_posts) attachment file (pdf, images) from a post when i have only inserted it from media library and it isn’t upload for it?
- How to let a role handle media without post permissions
- WordPress Gallery Permalink Structure Displaying Incorrectly
- How do I get just the title from wp_get_attachment_image
- List all files from subpages
- Add videos/images preview to bbPress attachment
- How to change an image source when rendering on frontend?
- Change attached img anchor link to post link
- attachment_fields_to_save first parameter is null
- How to open an attachment based on the date picker
- How to disable post-thumbnail from generation?
- How to Make Only Images Connected to a Post Show? Not Entire Media Collection?
- Resize $term attachment using url
- wp_get_attachment_image not to get the post_thumbnaill
- Extract url from next_image_link
- Lots of attachments is causing slow load
- WordPress Attachments vs Post Meta
- open image after clicking on single.php with different stylesheet in wordpress
- Attachment page custom title?