Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have videos set up as a custom post type and music, commercial, promo set up as categories within this post type:

I have a function that displays a custom meta box on the videos post type pages in the wordpress back end. The user is able to enter the ID of a youtube video or the ID of a vimeo video - wordpress then displays the video for the ID on custom post type page. When a user adds a new post to the videos custom post type and assigns it to any of the categories I specify I want wordpress to display the different videos. The code I have at the moment is not doing what I want it to do because it is displaying the same video on each post even though an ID hasnt been specified on some of them. For example on the music post page I have assigned it the category music and put a vimeo video ID which is displayed on the front end but then the same video is displayed for promo and commercial and I dont want that to happen. The loop I have running this is (in single-videos.php):

<?php

$args = array( 'post_type' => 'videos', 'posts_per_page' => 20, 'orderby' => 'date', 'order' => 'ASC' );
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post();
//$args = array( 'post_type' => 'videos', 'posts_per_page' => 20, 'orderby' => 'date', 'order' => 'ASC' );
$ytubeID = get_post_meta($post->ID, '_youtubeID', true);
$vimID = get_post_meta($post->ID, '_vimeoID', true);
if ($ytubeID || $vimID){
if ($ytubeID){

echo '<iframe title="YouTube video player" class="youtube-player" type="text/html" src="http://www.youtube.com/embed/'.$ytubeID.'"  allowfullscreen="true" frameborder="0" width="640" height="390">';

echo '</iframe>';
} elseif ($vimID){
echo '<br />';
echo '<iframe src="http://player.vimeo.com/video/'.$vimID.'" width="640" height="390" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
}//end if yutbeID or vimIDthe_excerpt(); //excerpt added for information
}

endwhile;
                    wp_reset_query();

?>

Below is the full function that creates the meta box & stores the information:

// Create the Video Information Meta Box by hooking into the admin menu for a post
    add_action('admin_menu', '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, $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(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

    } //end save_video_meta

    //save the video custom fields
    add_action('save_post', 'save_video_meta', 1, 2);
share|improve this question
    
is category means default taxonomy category or it's a custom taxonomy? –  Anjum Sep 27 '13 at 9:05
    
@anjum this is a custom taxonomy (i think) here is the code that generates the custom post type and the category: ` // V Custom Post Type add_action( 'init', 'create_videos' ); function create_videos() { register_post_type( 'videos', array( 'labels' => array( 'name' => __( 'Videos' ), 'singular_name' => __( 'Video' ) ), 'public' => true, 'taxonomies' => array("category"), 'has_archive' => true, 'supports' => array( 'title', 'editor', 'thumbnail' ), ) ); }` –  Neelam Khan Sep 27 '13 at 9:56
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks @cybnet I tried your suggestion but I still have an issue where every single video I add is displayed - wordopress is ignoring the fact that I want commercial videos posted with the commercial category and music videos posted with the music category. My loop is incorrect because it is not working the way I want it to. –  Neelam Khan Sep 27 '13 at 9:41
    
Please, 1) reply to other users bellow their comments, 2) add new code and new important information in the question itself (click the edit link) because it is difficult to read in the comments and because the new code/information will be available to other people who want to help you and read your question. That being said, can you post the code you are using to store the custom fields value? And not, the "categories" you are using are not a custom taxonomy, it is the "category" taxonomy built-in in Wordpress. –  cybnet Sep 27 '13 at 9:53
    
Oops sorry @cybnet will bear this in mind for future. I have added the function that stores the custom fields value into my question. –  Neelam Khan Sep 27 '13 at 9:59
    
Don't worry, I just give you some common rules here to make easier to follow the flow of the question/answers/comments. I see errors in the storing process, but it need you post it full, please. –  cybnet Sep 27 '13 at 10:02
    
I have added the full function for you (: –  Neelam Khan Sep 27 '13 at 10:16
show 2 more comments

Your Answer

 
discard

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

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