Note that there are some explanatory texts on larger screens.

plurals
  1. POSingle query for selecting a random images from X unique parents?
    primarykey
    data
    text
    <p>I've used this query to randomly select a total of <code>$limit</code>-images (attachments), each from a unique and randomly selected parent. </p> <pre><code>$query="SELECT {$wpdb-&gt;posts}.post_parent, {$wpdb-&gt;posts}.ID FROM {$wpdb-&gt;posts} INNER JOIN {$wpdb-&gt;term_relationships} ON ({$wpdb-&gt;posts}.post_parent = {$wpdb-&gt;term_relationships}.object_id) INNER JOIN {$wpdb-&gt;term_taxonomy} ON ({$wpdb-&gt;term_relationships}.term_taxonomy_id = {$wpdb-&gt;term_taxonomy}.term_taxonomy_id) WHERE {$wpdb-&gt;posts}.post_type = 'attachment' AND {$wpdb-&gt;term_taxonomy}.taxonomy = 'category' AND {$wpdb-&gt;term_taxonomy}.term_id IN ('{$wpdb-&gt;escape($category_filter)}') AND {$wpdb-&gt;posts}.post_password = '' AND {$wpdb-&gt;posts}.post_mime_type IN ('image/jpeg', 'image/gif', 'image/png') GROUP BY {$wpdb-&gt;posts}.post_parent ORDER BY {$order_by} LIMIT {$limit};"; </code></pre> <p>(<a href="http://wordpress.org/extend/plugins/bulk-select-featured-image/" rel="nofollow noreferrer">full code at pastebin</a>)</p> <p>Unfortunately it has three faults: </p> <ol> <li><p>I think the password-check is incorrect as it tests the attachment and not the parent_post, right? (does WordPress even support password protected gallery attachments?)</p></li> <li><p>it certainly doesn't check the parent for <code>post_status = "publish"</code></p></li> <li><p>it correctly selects random posts, but always the same pictures within them (the first one). </p></li> </ol> <p>So - I humbly ask for your SQL-fu. How does one <em>both</em> select a random parent (first checking for published status) and then a random image ID owned by that parent, all in one query?</p> <p>(I could select <em>all</em> attachments, ordered randomly and loop through them all and just grab the first <code>$limit</code>from unique parents. But that leads to parents with lots of images getting selected too often.)</p> <hr> <p><em>"When the answer escapes you, change the question..."</em> </p> <p>I just released <a href="http://wordpress.org/extend/plugins/bulk-select-featured-image/" rel="nofollow noreferrer">a plugin</a> that enables bulk selection of a “featured post image”, directly from the media library. The core of my function went from ~45 lines to this;</p> <pre><code>foreach($potential_parents as $parent){ if(has_post_thumbnail($parent-&gt;ID)) { $image = get_post_thumbnail_id($parent-&gt;ID); } } </code></pre> <p>It's not random but the site looks better and visitors have an easier time navigating content now that the thumbnails are consistent. </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.
    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