0

Is it possible to query / get latest post that has some youtube video embedded in its content?

I know how to do it with raw SQL but I'm not sure how to do such thing with wp_query.

1 Answer 1

2

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).

5
  • wrote an answer an noticed it is basically a duplicate of your first item. so instead will just comment that you can make it more robust by using ://youtube instead of the plain youtube Dec 11, 2016 at 16:38
  • yes it might be better to narrow down the search query and then there are all the various types of youtube links supported, I think we have like www.youtube.com, youtube.com, youtu.be, m.youtube.com and what not ;-) and now the various 3rd party youtube shortcodes comes to mind as well ;-) @MarkKaplun
    – birgire
    Dec 11, 2016 at 16:45
  • 1
    btw if you comment and there is only one additional user on the thread, you don't have to specify him as far as I know Dec 11, 2016 at 16:48
  • aha ok thanks, I'm not always sure how the notification system works: like my misunderstanding here shows ;-)
    – birgire
    Dec 11, 2016 at 16:50
  • I discovered it about a month ago :) Dec 11, 2016 at 16:58

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.