WordPress: meta attachment request (image size)

Is it possible to use WP_Query to get attachment images by pixel size?

For example, all images are 500 pixels wide and 300 pixels high. Or images with a height of more than 300 pixels.

As far as I can see, I can catch this data in a meta request using 'key' => '_wp_attachment_metadata' , but what then? There seems to be no solution that is accurate enough to target that width or height inside _wp_attachment_metadata ...

+5
source share
2 answers

You cannot do this with wp_query, since the height and width do not have their own meta-fields (they are part of a serialized array). But this is easy enough to overcome, we can simply assign them our own postmeta db entry at boot time (you can also use wp_query to get all the images and cycle through to update existing images)

 add_filter('wp_generate_attachment_metadata', 'add_metac', 10, 2); function add_metac($meta, $id){ update_post_meta($id, 'height', (int) $meta['height']); update_post_meta($id, 'width', (int) $meta['width']); return $meta; } 

Then you can request larger images, etc., for example:

 $types = array( 'image/jpeg', 'image/gif', 'image/png'); $args= array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => $types, 'meta_query' => array( 'relation' => 'AND', //-->this is default but showing here as you can use OR array( 'key' => 'height', 'value' => 300, 'type' => 'numeric', 'compare' => '>', ), array( 'key' => 'width', 'value' => 300, 'type' => 'numeric', 'compare' => '>', ), ) ); $images= new WP_Query($args); 
+1
source

@Dan is correctly specified by WP_Query, and mysql cannot handle this way. But we need an entire display with a conditional size above @Dan that meets this solution, but when we also need the pagination option and the display of limits on each page, so you have a solution below the request for a change request.

 <?php global $query_args; $query_args = array( 'post_type' => 'attachment', 'post_mime_type' => array('image/jpeg', 'image/gif', 'image/png'), 'post_status' => 'inherit', 'fields' => 'ids' ); // add pagination parameters in $query_args add_filter( 'posts_request', function( $request ){ global $wpdb, $query_args; remove_filter( 'posts_request', __FUNCTION__ ); $all_media = $wpdb->get_results($request); if( empty( $all_media ) ) return $request; $exclude_ids = array(); foreach( $all_media as $media ){ $meta = wp_get_attachment_metadata( $media->ID ); if( $meta['width'] != 500 || $meta['height'] < 300 ) // Add condition also allow add more condition with different sizes $exclude_ids[] = $media->ID; } if( $exclude_ids ){ $query_args['post__not_in'] = $exclude_ids; $q = new WP_Query( $query_args ); $request = $q->request; wp_reset_query(); } return $request; }); $query = new WP_Query( $query_args ); //echo $query->request; if( $query->have_posts() ){ while( $query->have_posts() ){ $query->the_post(); echo get_the_title(); } } wp_reset_query(); 

I have not tested any more records. I hope this helps you.

0
source

All Articles