• I am trying to create a ‘latest video’ section. Basically, what I’m trying to do is get the custom field value (url of the video) from the latest post in the ‘video’ post format. Then, I’m trying to insert that url into the YouTube embed code and echo the entire embed code with the url included. Below is a code that I managed to put together. The $video_url variable is coming up as an empty string for some reason. What’s wrong with my code?

    <?php
    	$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' => '!='
    			)
    		)
    	);
    	$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>';
    ?>
Viewing 1 replies (of 1 total)
  • Thread Starter wpnewbie69

    (@wpnewbie69)

    Figured it out.

    This:
    $video_url = htmlspecialchars(get_post_meta(get_the_ID($latest_video_id), 'dt_video', true));

    Should have been this:
    $video_url = htmlspecialchars(get_post_meta($latest_video_id, 'dt_video', true));

Viewing 1 replies (of 1 total)
  • The topic ‘Echo the custom field value from the latest post from the 'video' post format?’ is closed to new replies.