WordPress custom fields for custom post type


/*Show data fields in metabox*/
add_action( ‘admin_init’, ‘my_admin_meta_data’ );
function my_admin_meta_data() {
add_meta_box( ‘custom_meta_box’,
‘Price Information’,
‘display_price_meta_box’,
‘custom-post’, ‘normal’, ‘high’
);
}

function display_price_meta_box($post_data){
$price = esc_html( get_post_meta( $post_data->ID, ‘price’, true ) );
?>

<?php
}

/*Save custom post data*/
add_action( ‘save_post’, ‘save_data_function’, 10, 2 );

function add_property_fields($post_id, $post_data){
if ( $post_data->post_type == ‘custom-post’ ) {
if ( isset( $_POST[‘price’] ) && $_POST[‘price’] != ” ) {
update_post_meta( $post_id, ‘price’, $_POST[‘price’] );
}
}
}
?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s