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 used the code below to create a metabox for posts. These meta boxes are also seen in the custom fields.

I do not want these meta boxes to show in custom fields. Is there any way to not show meta boxes in custom fields?

$meta_boxes_video = 
array(
"hotel name1" => array(
"name" => "hotel_name1",
"type" => "input",
"title" => "<strong>Hotel Name</strong> (Required)",
"description" => ""),
"price1" => array(
"name" => "price1",
"type" => "input",
"title" => "<strong>Price</strong>",
"description" => ""),
"hotel name2" => array(
"name" => "hotel_name2",
"type" => "input",
"title" => "<strong>Hotel Name</strong> (Required)",
"description" => ""),
"price2" => array(
"name" => "price2",
"type" => "input",
"title" => "<strong>Price</strong>",
"description" => ""),
"hotel name3" => array(
"name" => "hotel_name3",
"type" => "input",
"title" => "<strong>Hotel Name</strong> (Required)",
"description" => ""),
"price3" => array(
"name" => "price3",
"type" => "input",
"title" => "<strong>Price</strong>",
"description" => ""),
"hotel name4" => array(
"name" => "hotel_name4",
"type" => "input",
"title" => "<strong>Hotel Name</strong> (Required)",
"description" => ""),
"price4" => array(
"name" => "price4",
"type" => "input",
"title" => "<strong>Price</strong>",
"description" => ""),
"hotel name5" => array(
"name" => "hotel_name5",
"type" => "input",
"title" => "<strong>Hotel Name</strong> (Required)",
"description" => ""),
"price5" => array(
"name" => "price5",
"type" => "input",
"title" => "<strong>Price</strong>",
"description" => ""),
"hotel name6" => array(
"name" => "hotel_name6",
"type" => "input",
"title" => "<strong>Hotel Name</strong> (Required)",
"description" => ""),
"price6" => array(
"name" => "price6",
"type" => "input",
"title" => "<strong>Price</strong>",
"description" => ""),
"img_name1" => array(
"name" => "img_name1",
"type" => "input",
"title" => "<strong>Img1</strong> (Required)",
"description" => ""),
"img_name2" => array(
"name" => "img_name2",
"type" => "input",
"title" => "<strong>img2</strong> (Required)",
"description" => ""),
);


function display_video_meta_boxes() {
global $post, $meta_boxes_video;
    foreach($meta_boxes_video as $meta_box) {

        echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';

        echo'<h2>'.$meta_box['title'].'</h2>';

        if( $meta_box['type'] == "input" ) { 

            $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);

            if($meta_box_value == "")
                $meta_box_value = $meta_box['std'];
            $meta_box_value = str_replace("\"","'",$meta_box_value);

            echo'<input type="text" name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" size="55" /><br />';

        } elseif( $meta_box['type'] == "textarea" ) { 

            $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);

            if($meta_box_value == "")
                $meta_box_value = $meta_box['std'];
            $meta_box_value = str_replace("\"","'",$meta_box_value);

            echo'<textarea name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" style="width:100%" cols="20" rows="1">'.$meta_box_value.'</textarea><br />';

        } elseif( $meta_box['type'] == "button" ) { 

            $meta_box_value = "Add New Source";

            if($meta_box_value == "")
                $meta_box_value = $meta_box['std'];
            $meta_box_value = str_replace("\"","'",$meta_box_value);
            echo '<script type="text/javascript">
            function test()
            {
            alert("fgfg");
            var element = document.createElement("textarea");
            element.setAttribute("value", "Dfdsf");
            element.setAttribute("name", "sdfdf");
            var foo = document.getElementById("foo");
            foo.appendChild(element);
            }
            </script>';
            echo '<div id="foo">';
            echo'<input type="button" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" onclick="test();"  /><br /></div>';

        }
        elseif ( $meta_box['type'] == "select" ) {

            echo'<select name="'.$meta_box['name'].'_value">';

            foreach ($meta_box['options'] as $option) {

                echo'<option';
                if ( get_post_meta($post->ID, $meta_box['name'].'_value', true) == $option ) { 
                    echo ' selected="selected"'; 
                } elseif ( $option == $meta_box['std'] ) { 
                    echo ' selected="selected"'; 
                } 
                echo'>'. $option .'</option>';

            }

            echo'</select>';

        } elseif ($meta_box['type'] == "info") {


            echo '<p><strong>Add your property images using the "Upload/Insert" button above the content textbox..</p>';
        }

        echo'<p><label for="'.$meta_box['name'].'_value">'.$meta_box['description'].'</label></p>';
    }

}

function create_video_meta_box() 
{
    if (function_exists('add_meta_box')) 
    {
        add_meta_box( 'ameta_boxes', 'Extra Information', 'display_video_meta_boxes', 'post', 'normal', 'high' );
    }
}

function save_video_postdata( $post_id ) {
    global $post, $meta_boxes_video;  

    if( get_post_type() == 'post' ) {
    foreach($meta_boxes_video as $meta_box) {  

        // Verify  
        if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {  
        return $post_id;  
        }  

        if ( 'page' == $_POST['post_type'] ) {  
        if ( !current_user_can( 'edit_page', $post_id ))  
        return $post_id;  
        } else {  
        if ( !current_user_can( 'edit_post', $post_id ))  
        return $post_id;  
        }  

        $data = stripslashes($_POST[$meta_box['name'].'_value']);  

        if(get_post_meta($post_id, $meta_box['name'].'_value') == "")  
        add_post_meta($post_id, $meta_box['name'].'_value', $data, true);  
        elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))  
        update_post_meta($post_id, $meta_box['name'].'_value', $data);  
        elseif($data == "")  
        delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));  
        }
    }

}


add_action('admin_menu', 'create_video_meta_box');
add_action('save_post', 'save_video_postdata');
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

All post meta is shown in the custom fields, unless it begins with an underscore.

So to 'remove' post meta from the custom fields metabox, you simply have to give it a name, beginning with an underscore. You'll then be responsible for saving/updating the post meta (adding your own metabox where necessary).

share|improve this answer
    
Correct. I would also recommend you to not use too much custom field keys. You cann pass arrays to the specific functions (get_post_meta, ...) to save queries. For example, use 1 array hotel_1 with the children price, name, etc. WordPress automatically (de-)serializes data in custom fields, so it's not a heavy adjustment. –  swissspidy May 15 '12 at 18:17
    
@PascalBirchler - you should really add post meta as separate rows, otherwise it because impossible to query posts by the individual meta keys. Also, lumping them together in array does not save on queries. When get_post_meta is used, all post meta is pulled from the post and stored in the object cache - so any subsequent calls do not query the db. –  Stephen Harris May 15 '12 at 18:21
    
Well, yeah, it depends on how you're going to use the meta keys. Sorting is obviously wanted in this case. And let me correct my part about the queries: it also prevents some unnecessary work, because you're doing everything twelve times. –  swissspidy May 15 '12 at 18:27
add comment

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.