0

I've been working on this issue for quite a while and am truly stumped. Below is all of the code for my custom post type:

/* Services Post Type */

function services_post_type() {
$labels = array(
'name' => _x( 'Services', 'service' ),
'singular_name' => _x( 'Service', 'service' ),
'add_new' => _x( 'Add New', 'service' ),
'add_new_item' => _x( 'Add New Service', 'service' ),
'edit_item' => _x( 'Edit Service', 'service' ),
'new_item' => _x( 'New Service', 'service' ),
'view_item' => _x( 'View Service', 'service' ),
'search_items' => _x( 'Search Services', 'service' ),
'not_found' => _x( 'No services found', 'service' ),
'not_found_in_trash' => _x( 'No services found in Trash', 'service' ),
'parent_item_colon' => _x( 'Parent Service:', 'service' ),
'menu_name' => _x( 'Services', 'service' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'supports' => array( 'title', 'editor' ),
'taxonomies' => array( 'category' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type( 'service', $args );
} 
add_action( 'init', 'services_post_type' );

/*
 * Custom meta boxes 
*/

add_action( 'add_meta_boxes', 'create_meta_boxes' );

function create_meta_boxes() {
add_meta_box( 'my-meta-box-id', __('Service Price'), 'meta_box_info', 'service', 'normal', 'low' );
}


// Create meta box: Service Price

function meta_box_info( $post ) {

$values = get_post_custom( $post->ID );

wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>

<?php $text = get_post_meta($post->ID, 'meta_box_info', true); ?>
<p>Price: <input type="text" name="meta_box_info" id="meta_box_info" size="25" /></p>

<?php   
}

// Save meta box: Service Price

function save_my_metadata($ID = false, $post = false)
{
if($post->post_type != 'service')
    return;
update_post_meta($ID, 'my_metadata', $_POST['my_metadata']);
}
  • 2
    (A) You didn't attach it to a hook - is this missing just in here? (B) Look at examples for the save_post action. – kaiser Oct 3 '13 at 2:13
1

As kaiser pointed out, you need to use the save_post hook to call your save_my_metadata() function.

See this code sample for a complete example of saving (and validating) metabox data:

https://github.com/Veraxus/nv-example-meta-box/blob/master/init.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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