Question

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",
    OBJECT
);

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top