Query posts that contain video?

I don’t think there’s a fool-proofed way to do that, because the YouTube videos can be embedded in various ways and later be removed from the posts’ content.

Here are few ideas:

  • If you mean searching for e.g. the youtube string, then you could use the s argument in WP_Query. But the links might be inline and not displaying the YouTube player. The embeds might also contain youtu.be instead, etc.

  • For oEmbeds the HTML is cached in the post meta table. You might look in there, but it might be slow and the meta values might not be in sync with the post content.

  • If you’re searching for some specific strings in the content, then the generated SQL query from WP_Query can be modified through filters, like posts_where posts_clauses and posts_search.

  • When you update the posts, you might look for any embed code and tag it, if it a contains a YouTube embed. Similarly un-tag it it doesn’t. This could be done automatically (some code needed for that) through hooks like save_post. Then you would search for the tag (or a term in a custom taxonomy) that should scale better than meta queries (that would be needed, if you prefer to store it in the post meta table).