0

I need to get attachment images with horizontal orientation. I tried to use Meta_Query but I don't know how to compare two values (width and height) from one associative array that is in wp_postmeta table. I don't have any specific values of width and height, I only need to compare this values which is in associative array.

Also tried to do this with $wpdb custom query:

$attach_ids = $wpdb->query( "
SELECT wp_posts.ID FROM wp_posts, wp_postmeta 
WHERE EXISTS (SELECT * FROM wp_postmeta 
    WHERE meta_key = '_wp_attachment_metadata' 
    AND meta_value= ???)
AND wp_posts.ID = wp_postmeta.post_id 
AND wp_posts.post_type = 'attachment' 
AND wp_posts.post_mime_type = 'image/jpeg'
ORDER BY RAND() LIMIT 0, 6" );

Can anyone understand my problem and help? P.S. Sorry for my English.

0

The _wp_attachment_metadata stores serialized data. It's not meant to be used directly in queries to match results. Instead hook add_attachment action and store original image's dimensions into separate post meta properties. After you're happy with how it works follow Rarst's advice and find a way to walk all existent image attachments and generate those meta data for them also.

0

Unfortunately attachment data is stored as single array entry, which gets inserted into database as serialized PHP string. This is an extremely inconvenient approach for querying it with SQL.

Since you seem to want randomized results run repeatedly, I think the best approach would be to walk through attachments with code and create new custom meta holding individual width and height values from them. After that do the same on new images getting uploaded and it will be much more friendly to query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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