Speeding up Media Tags with WP_Query
-
I’ve got a media heavy site and found
get_attachments_by_media_tags()
to be slow when querying for image tags. I had been trying cache the queries using transient caching, but found that to be more of a temporary fix.I decided to try and use
WP_Query
yesterday, and found it to be rather easy and much faster. I didn’t benchmark to see how much faster, but it is a noticeable difference.Here’s what I did:
<?php $media_args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'tax_query' => array( array( 'taxonomy' => 'media-tags', 'field' => 'slug', 'terms' => YOUR-MEDIA-TAG-SLUG-HERE ) ) ); $media_items = new WP_Query($media_args); if ($media_items->have_posts()) { while ($media_items->have_posts()) { $media_items->the_post(); $media_image = wp_get_attachment_image_src($post->ID); ?> <img src="<php echo $media_image[0]; ?>" /> <?php } } ?>
Would be interested to see if anyone has tried anything similar and what their results were?
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Speeding up Media Tags with WP_Query’ is closed to new replies.