I am working on a plugin for generating podcast feeds, and I'm trying to figure out if it is possible to use WP_Query to pull only posts from a specified category that have audio attachments. I know I can do this in a custom query, for example:

$podeps = $wpdb->get_results(
    "SELECT SQL_CALC_FOUND_ROWS $wpdb->posts.*
        FROM $wpdb->posts,wp_term_relationships
        WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id
            AND $wpdb->term_relationships.term_taxonomy_id IN ($catid)
            AND $wpdb->posts.post_type = 'post'
            AND $wpdb->posts.post_status = 'publish'
            AND $wpdb->posts.ID IN (
                SELECT DISTINCT post_parent
                FROM $wpdb->posts
                WHERE post_parent > 0
                    AND post_type = 'attachment'
                    AND post_mime_type = 'audio/mpeg'
        GROUP BY $wpdb->posts.ID
        ORDER BY $wpdb->posts.post_date DESC
        LIMIT 50",

But is there a way to do this using WP_Query? If possible, I would like to refrain from having to write the query directly, so as to ensure broadest compatiblity. Thanks!

  • Are the attachments connected to their parent besides being a child? ie, are they enclosured in that post? (check wp_postmeta for that meta_key) If so, you could query only for posts that have this key. If not, I doubt there is a neat solution with pure WP functions. What you can do: query for the attachments, save unique post_parent IDs - et voila
    – kero
    Jul 10, 2017 at 17:16
  • Currently, the plugin is set to primarily detect the attachments as children, since I have not always been able to rely on the audio file being inserted into the post body. However, you raise an interesting point about using post metadata such as the enclosure. That would probably be the most useful. Thanks! Jul 10, 2017 at 18:02
  • I was looking how WP manages attachments/children. And it seems the parent does not get a postmeta entry by default. Maybe you could add your custom entry via a hook (when attachment is uploaded and is child -> write something in parent postmeta)
    – kero
    Jul 11, 2017 at 8:53
  • I did something similar. When the post is saved, the plugin checks for enclosure data and copies/repurposes it into a postmeta entry. Failing that, it checks for attachments to the post and generates the postmeta. Jul 12, 2017 at 15:43
  • And then you query for that postmeta key? If this answers your original question here, feel free to answer it yourself
    – kero
    Jul 13, 2017 at 8:34

1 Answer 1


From the research I was doing, and from hearing from Kero in the comments, it would appear that querying for posts with attachments via WP_Query isn't possible in a concise manner.

With that in mind, I changed my podcasting plugin to no longer pull posts with attachments (since that could ultimately be unreliable anyway), and instead save the podcast enclosure data in postmeta. That way, I can use WP_Query and just add a meta_query to the pull.

If you'd like to see the plugin as is, you can find it in this GitHub repo.

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.