I want to pull one random image and use it as a background image —- but ONLY if it's a landscape image.

I found this post:


which is a pretty good explanation of how to start here.

For my purposes, landscape can be as simple as width > height (any aspect ratio greater than 1).

But I'm not totally sure how to add that as part of the metadata and then query for it (based on the above answer.)


  • The linked resource is good but the query relies on fixed values for width and height. The best IMHO would be to define ( or pass as parameter ) a 'min' value for the image width you want to use (since you're using as background of something) and than define a 'ratio' parameter to determine the height (I guess a certain difference between width and height would be a better design result than an almost square image) + finally define a fallback image in case no image match the condition. In this case I can help writing a function if still needed Jul 26, 2018 at 7:08
  • @AndreaSomovigo Yes I for sure that query finds a fixed min width and height. But it gives a good overview of how one would add a certain piece of meta data to an image attachment and then query for that. What I need help with is writing the function that adds “ratio” to the image meta data. Then I can query for that. I think that’s a simple enough way to do it. I tried my hand at it but I’m not sure I added the meta data correctly.
    – Tomasch
    Jul 26, 2018 at 12:46

1 Answer 1


ok, following the comments:

//from https://stackoverflow.com/questions/37537577/wordpress-query-by-attachment-meta-image-size

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']);
update_post_meta($id, 'ratio', round ($meta['width']  / $meta['height'] , 2 ));
return $meta;


at this point you will have, for the newly uploaded images, in the postmeta table all the information enter image description here and a ratio >1 = horizontal

EDITED some typo and completed with loop

function stack_309636_get_horizontal_bg( $width=900 , $ratio=1 ){
  $types = array( 'image/jpeg', 'image/gif', 'image/png');
  $args= array(
    'post_type'         => 'attachment',
    'post_status'    => 'inherit',
    'posts_per_page' =>   1,
    'orderby'      =>'rand',
    'post_mime_type'    => $types,
    'meta_query' => array(
        'relation' => 'AND', 
                'key'     => 'width',
                'value'   => $width, //the minimum required
                //'type'    => 'numeric',
                'compare' => '>',
                'key'     => 'ratio',
                'value'   => $ratio,  //eventually 1.5 or higher
                //'type'    => 'numeric',
                'compare' => '>',


  $query= new WP_Query($args);

  if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $img = wp_get_attachment_image_src( $query->post->ID,'full');                            
    $return="your_default_image_url"; // in case no images match 

  return $return;

echo stack_309636_get_horizontal_bg(1200, 1.5);
  • Thanks; I had to edit this a bit to get it to work for my purposes. I'll have to post it later when I get a chance. I'm also trying to restrict this query so that the attachments only come from a custom post type "projects."
    – Tomasch
    Jul 27, 2018 at 3:06
  • in this case you need a previous query to retrieve CPT IDS and add 'post_parent__in' => $previous_query->posts in the second query Jul 27, 2018 at 9:10
  • Not sure why I can't get this to work. I have images where the ratio is greater than 1 (imgur.com/Zp0EzAl) but they won't return. The query will return a less than ratio but nothing for the greater than. Any ideas?
    – Tomasch
    Aug 1, 2018 at 0:34
  • First of all consider that this works for the newly uploaded img, the older don't have the custom meta Aug 1, 2018 at 4:19
  • for sure. But I even deleted everything in my media library and reuploaded. Now I only have two images in my library — one with a ratio of .67 and one with 1.5. It won’t ever return the 1.5. Maybe there’s something wrong with my Wordpress install? I can’t understand why it wouldn’t return this otherwise.
    – Tomasch
    Aug 1, 2018 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.