I would like to construct a query to show a list of custom posts ("podcasts") with the urls to their respective attachments. Each custom post_type ("podcast") has 1 attachment (the mp3 file belonging to it's post_parent). I would like an unordered list showing podcast's title, and it's attachment url (guid of the attachment) like this:
<ul>
<li>Podcast Title - http://..../podcast.mp3</li>
I achieved this with wp queries:
<?php query_posts('showposts=1&post_type=podcasts'); ?>
<ul>
<?php while (have_posts()): the_post(); ?>
<li><?php the_title(); ?><br>
<?php
$args = array( 'post_type' => 'attachment', 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
echo $attachment->guid;
}
} ?>
</li>
<?php endwhile; ?>
</ul>
But I would like to know how I solve with raw SQL query. I've tried so many queries it's made me dizzy - any help would be amazing!
* EDIT: Decided to go with John's suggestion of WordPress' queries. Thank you and I trust someone else will also find this useful in future.
WP_Query
queries are dizzying, wait till you see the raw SQL.