I need to include attachments in the search. So far, I have managed this by hooking into pre_get_posts and checking if it's on the search results page, I add 'attachment' to the post_type and 'inherit' to the post_status, in order to also include attachments that are not attached to posts.

However, only attachments that have the custom taxonomy download_category set to any term should be included, attachments that do not have any download_category may not be seen in the search results.

How can I achieve that?

I have found this answer Custom Taxonomy WP_Query for All Terms in a Taxonomy? and tried it out. While this does really only show those attachments with any download_category set, it applies to posts and pages as well, and as those never have the download_category taxonomy set, no pages and posts are shown in the search results...

So to clarify again what I need in short terms: - include attachments in the search results, but only if they have taxonomy download_category set to any term - Keep normal search functionality for pages and posts!

  • I have now been able to achieve what I need by hooking into posts_clauses and modifying the where and join clauses with regular expressions (ouch!). This is hacky as hell and I don't want to rely on it for the future, when a WP Update or additional plugin might change the basic query in a way that will break the whole functionality. Isn't there a simpler way? – Constantin Groß Jan 30 '15 at 12:17
  • Another problem with the approach in my previous comment is that pagination is not working any longer... posts_found stays at a maximum of 10... O.o – Constantin Groß Jan 30 '15 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.