1

I need to add only specific attachments (PDF) to the query along with other CPT. But when I add mime type value to the query any other post type get filtered out.

Here is the query args

$args = [
    'post_type' => ['any', 'attachment'],
    'post_status' => ['publish', 'inherit'],
    'post_mime_type' => ['application/pdf'],
    'posts_per_page' => 10
];

How do I get other post types with attachments of specific mime type? (only pdf in this case)

1 Answer 1

1

First of all if any is used for post_type field there can not be any other value given in array like in the question. Below is the modified args

$args = [
    'post_type' => array_values(get_post_types(['public' => true])),
    'post_status' => ['publish', 'inherit'],
    'posts_per_page' => 10
];

Use posts_where filter

add_filter('posts_where', 'add_search_mime_types');
$query = new WP_Query( $args );
remove_filter('posts_where', 'add_search_mime_types');


function add_search_mime_types($where){
    $where .= ' AND post_mime_type IN("application/pdf","") ';
    return $where;
}

The empty string on the SQL query to include post types other then attachment.

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.