0

I am trying to create a 'latest video' section. Basically, what I'm trying to do is get the custom field value (video url) from the last post in the 'video' post format. Then, I'm trying to insert that url into the YouTube embed code and echoing the entire embed code with the url included. Below is a code that I managed to put together. I'm sure there are some things wrong with it and was wondering if someone could help me make sense of this.

enter image description here

<?php   
    $args = array( 'numberposts' => '1', 'tax_query' => array(
            array(
                'taxonomy' => 'post_format',
                'field' => 'slug',
                'terms' => 'post-format-video'
            )
    ) );
    $latest_video = wp_get_recent_posts($args); // Get latest video in 'video' post format
    $latest_video_id = $latest_video['0']['ID']; // Get latest video ID
    $video_url = htmlspecialchars(get_post_meta(get_the_ID($latest_video_id), 'dt_video', true));
    echo '<iframe width="180" height="101" src="'.$video_url.'?rel=0" frameborder="0" allowfullscreen></iframe>';
?>

Edit: $video_url is returning an empty string.

2
  • We need more info. How is this custom field created? (It looks like some Plugin) Do you want to embed that Youtube video as video? What do you mean by "the entires site"?
    – kraftner
    Mar 26, 2014 at 10:33
  • @kraftner I am not sure how the custom field is created but it's through the theme and not through a plugin. I did a search through the theme files and found an instance of "dt_video" in the functions file which I'm not sure helps (line 331): pastebin.com/72sqzDXY Yes, I want the YouTube video to be shown as an embedded video. What I mean is that literally my entire site is shown in the iframe instead of the YouTube video.
    – Desi
    Mar 26, 2014 at 10:39

1 Answer 1

2

While this may not answer your question, but it will be helpful to solve it. It seems like the statement

 $video = get_post_meta($postid, 'dt_video', true); 

is assigning blank string value to $video variable and thus iframe code becomes

<iframe width="180" height="101" src="?rel=0" frameborder="0" allowfullscreen></iframe>

which is the cause of your site being displayed in the iframe.

And for your question:

I want to show the latest video posted which is decided by the latest post with the video url custom field filled in

I would suggest you to paste your code for selecting posts from db throug query, so I can have insight.

Edit:

change your query args parameter to exclude blank values in dt_video in post meta field.

$args = array( 
    'numberposts' => '1', 
    'tax_query' => array(
        array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => 'post-format-video'
        )
    ),
    'meta_query' => array(
        array(
            'key' => 'dt_video',
            'value' => '',
            'compare' => '!='
        )
    )
);

check WordPress documentation for more information :http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

5
  • I just edited my post with an updated code and a description that hopefully makes more sense. Yes, the $video variable (now updated to $video_url) seems to be showing a blank string value. I am not sure of your last sentence. I don't think I have that code.
    – Desi
    Mar 26, 2014 at 11:21
  • Please check my edited answer.
    – Dharmang
    Mar 26, 2014 at 13:22
  • I changed my query args to match yours but $video_url is still returning an empty string. The output looks like this: http://mysite.com/?rel=0
    – Desi
    Mar 26, 2014 at 17:09
  • Is this line set up correctly? $video_url = htmlspecialchars(get_post_meta(get_the_ID($latest_video_id), 'dt_video', true)); I am not sure if I'm allowed to use a variable as an argument in get_the_ID().
    – Desi
    Mar 26, 2014 at 17:52
  • Got it working. Turns out that line should have been $video_url = htmlspecialchars(get_post_meta($latest_video_id, 'dt_video', true)); I'll give you the check anyway since you helped me set up my arguments.
    – Desi
    Mar 26, 2014 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.