Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not <em>one</em> query, but it's a heck of a lot faster than the WordPress-hackery I posted earlier, and not <em>very</em> much slower than the original. I suppose it's the price to pay for correctness and while being SQL-ignorant. :P </p> <pre><code>$potential_parents = $wpdb-&gt;get_results( "SELECT DISTINCT {$wpdb-&gt;posts}.ID, {$wpdb-&gt;posts}.post_title FROM {$wpdb-&gt;posts} LEFT JOIN $wpdb-&gt;postmeta wpostmeta ON ({$wpdb-&gt;posts}.ID = wpostmeta.post_id) LEFT JOIN $wpdb-&gt;term_relationships ON ({$wpdb-&gt;posts}.ID = $wpdb-&gt;term_relationships.object_id) LEFT JOIN $wpdb-&gt;term_taxonomy ON ($wpdb-&gt;term_relationships.term_taxonomy_id = $wpdb-&gt;term_taxonomy.term_taxonomy_id) WHERE $wpdb-&gt;term_taxonomy.taxonomy = 'category' AND $wpdb-&gt;term_taxonomy.term_id IN({$wpdb-&gt;escape($category_filter)}) AND {$wpdb-&gt;posts}.post_type = 'post' AND {$wpdb-&gt;posts}.post_status LIKE 'publish' AND {$wpdb-&gt;posts}.post_password = '' ORDER BY {$order_by};"); $imglists = array(); $parent_titles = array(); $count = $limit; foreach($potential_parents as $parent){ $images = $wpdb-&gt;get_results( "SELECT {$wpdb-&gt;posts}.ID FROM {$wpdb-&gt;posts} WHERE {$wpdb-&gt;posts}.post_parent = {$parent-&gt;ID} AND {$wpdb-&gt;posts}.post_type = 'attachment' AND {$wpdb-&gt;posts}.post_mime_type IN ('image/jpeg', 'image/gif', 'image/png') ORDER BY {$order_by} LIMIT 1;"); if($images){ $imglists[$parent-&gt;ID] = $images; $parent_titles[$parent-&gt;ID] = $parent-&gt;post_title; if(--$count &lt; 1){break;} } } foreach($imglists as $parent_id =&gt; $imagelist){ $imgurl = wp_get_attachment_image_src($imagelist[0]-&gt;ID, $size); if($imgurl === false){continue;} //the image doesn't exist? $img_width = $imgurl[1]; $img_height = $imgurl[2]; $imgurl = $imgurl[0]; $selection[] = array('post_url'=&gt;get_permalink( $parent_id ), 'post_title' =&gt; wp_specialchars($parent_titles[$parent_id]),'post_id'=&gt;$parent_id, 'img_src'=&gt;$imgurl, 'width'=&gt;$img_width, 'height'=&gt;$img_height); } return $selection; </code></pre> <p>So basically, first grab one largish result set with all published posts in the categories. Then a bunch of smaller queries, fetching a single attachment ID every loop until the <code>$limit</code> has been filled.</p> <p>If you've got lots of posts in these categories without attachments, you'll waste some time here. But in our case it seems to be manageable. </p> <p>Still looking for that efficient single-query solution though. :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload