Your attempt was not to far away (+1 for trying, and crucially posting what you had tried), all you had to do really was add a compare of EXISTS
to your meta query.
The meta_query
parameter can be a bit of a beast to wrap your head around, and just in case you’ve not found it I’d recommend having a read of the Class Reference for WP_Query, specifically for Custom Field Parameters.
$args = array(
'posts_per_page' => 1,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
'order' => 'DESC',
'orderby' => 'post_date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'video',
'compare' => 'EXISTS'
),
array(
'key' => 'video',
'value' => '',
'compare' => '!='
)
)
);
$loop = new WP_Query($args);
if($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post();
$specials = get_post_meta($post->ID, 'video');
foreach($specials as $special): setup_postdata($special);
echo '<iframe width="699" height="269" src="http://www.youtube.com/embed/'.$special.'?controls=1&showinfo=0&rel=0" allowfullscreen></iframe>';
endforeach;
endwhile;
wp_reset_postdata();
endif;
Additional
As an added redundancy I’ve also added a check to ensure that the a video
custom field actually contains a value. While I believe that the WP post edit screen will not actually allow you to save an empty custom value, I can’t be sure that you are updating it that way so thought it best to include.
However, if you don’t want to include that check you can change the meta_query
parameter to this –
'meta_query' => array(
array(
'key' => 'video',
'compare' => 'EXISTS'
)
)