I think you are passing a wrong post ID value to the get_post_meta()
function. Try this:
$ytubeID = get_post_meta(get_the_ID(), '_youtubeID', true);
$vimID = get_post_meta(get_the_ID(), '_vimeoID', true);
Note:, as you are using a new instance of WP_Query, you don’t need wp_reset_query();
, use wp_reset_postdata();
instead.
Until you add the full code that you are using to store the value of the custom fields, I just can say that you are passing, again, the wrong post ID value to the update_post_meta()
and delete_post_meta()
functions.. Normally, the post ID is passed to the save function callback and $post->ID is wrong (also is wrong the $post->post_type checking you are doing). You are also passing a second parameter to the save callback but the save callback only accepts one parameter. Try something like:
add_action( 'save_post', 'my_save_postdata' );
function my_save_postdata($post_id){
$video_meta['_youtubeID'] = $_POST['_youtubeID'];
$video_meta['_vimeoID'] = $_POST['_vimeoID'];
foreach ($video_meta as $key => $value) { // Cycle through the $video_meta array
if( $_POST['post_type'] == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV
if(get_post_meta($post_id, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post_id, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post_id, $key, $value);
}
if(!$value) delete_post_meta($post_id, $key); // Delete if blank
}//endforeach video meta
}
You can make further improvements, but they are out of the scope of the answer, I give an exampe, this code:
if(get_post_meta($post_id, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post_id, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post_id, $key, $value);
}
if(!$value) delete_post_meta($post_id, $key); // Delete if blank
Can be:
if($value) {
update_post_meta($post_id, $key, $value);
} else {
delete_post_meta($post_id, $key); // Delete if blank
}
Because update_post_meta()
will check if the custom field already has a value and it will update it, and if the custom field has not a value (the key doesn’t exist) the function will call add_post_meta()
.
All the above within your code:
// Create the Video Information Meta Box by hooking into the admin menu for a post
// Add the meta boxes in the add_meta_boxes action hook, no in admin menu
add_action('add_meta_boxes', 'video_add_box');
//Function call by the "add_action" to tell it what to add, "video_information" and where "post"
function video_add_box(){
add_meta_box('video_information', 'Video Information', 'video_information', 'videos', 'normal', 'high');
}
//function to populate the meta box added above
function video_information(){
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="video_noncename" id="video_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
//adds the custom field _youtubeID plus some other stuff
$youtubeID = get_post_meta($post->ID, '_youtubeID', true);
if ( empty($youtubeID) ) {
$youtubeID = '';
}
//adds the custom field _vimeoID
$vimeoID = get_post_meta($post->ID, '_vimeoID', true);
if ( empty($vimeoID) ) {
$vimeoID = '';
}
//add the box
echo '<br />';
echo '<strong>Youtube ID:</strong> <input type="text" name="_youtubeID" value="' . $youtubeID . '" size="20" maxlength="30" />';
echo '<br />';
echo '<strong>Vimeo ID:</strong> <input type="text" name="_vimeoID" value="' . $vimeoID . '" size="20" maxlength="30" />';
echo '<br />';
} //end video_information function
//save_video_meta is called below with the action "save_post" and saves your IDs to the post
function save_video_meta($post_id) {
// the $post parameter you wass passing has any effect, the save
// action only accept one parameter. If you need all the post data beeing saved
// you can access to the global $post_data; or to the superglobal $_POST
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['video_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID )){
return $post->ID;
}
$video_meta['_youtubeID'] = $_POST['_youtubeID'];
$video_meta['_vimeoID'] = $_POST['_vimeoID'];
foreach ($video_meta as $key => $value) { // Cycle through the $video_meta array
if( $_POST['post_type'] == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV
if($value) {
update_post_meta($post_id, $key, $value);
} else {
delete_post_meta($post_id, $key); // Delete if blank
}
}//endforeach video meta
} //end save_video_meta
//save the video custom fields
add_action('save_post', 'save_video_meta', 1, 2);
Just a reminder: WordPress can handled a high amount of external videos providers, like youtube and vimeo. Just paste the video URL in the editor box and it will be displayed in the front-end automatically. Also, there is a built-in taxonomy, “post format”, that let you filter your posts by “video format”. In short, I think that register a new post type with custom meta boxes with the only purpose of displaying post of “video type” is not needed at all; you can achieve the same functionality with little extra code using built-in features.