WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to embed the last posted video (from a specific category) to the frontpage of my Wordpress site. Is there any shortcode (or widget, even) I can use to simply filter my posts by category, and have the content (in this case, an embedded .wmv or .m4v) displayed based on a "last posted" basis. I am currently able to get a simple static embed working, but I need the embed to be dynamic based on the last video posted. The video cannot be hosted on sites like youtube or vimeo; I need to have them hosted as video files on a server and embed them that way.

Thanks in advance.

share|improve this question

migrated from stackoverflow.com Dec 8 '11 at 13:42

This question came from our site for professional and enthusiast programmers.

up vote 0 down vote accepted

This should work, even outside of the loop:

$latestPost = get_posts('numberposts=1&category=YOUR_CATEGORY_ID');

The Wordpress function get_posts sorts by post date, newest-to-oldest by default, so you'd get the newest post in whatever category you choose. Also, even though you are only requesting one post, the response will still be an array:

echo $latestPost[0]->post_content;


Since the video embed code is being provided by a plugin that uses a shortcode, you'll have to use the function do_shortcode on the content. If there is text (content other than the video) you'll have to parse out the shortcode from the post_content.

share|improve this answer
Thank you for the quick reply. I have tried getting a similar method working, however when I echo the post contents, all I get is text. I am using the mediaelements plugin, which allows me to embed wmvs into posts. The embed works, but only when I view the actual post, not the "post contents". So when I echo the post contents to my frontpage, all that is displayed is the shortcode text (in this case, [video src="video.mp4"]). I'm really not sure if I'm overlooking something here, or if Wordpress just disallows the type of embedding I need for security and this is not possible. – David Dec 7 '11 at 19:35
Ah! In that case, use the function do_shortcode to get the actual embed. I would imagine that you'll have to parse out the shortcode, in case the post has some text too. – The Maniac Dec 7 '11 at 19:53
Awesome! Thanks alot for the help. I never knew Wordpress had such a large library of functions and calls. Looks like I have some reading to do over the holidays. – David Dec 7 '11 at 20:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.