다른 CPT의 모든 게시물 ('제품')을 반복하는 metabox를 만듭니다. metabob은 해당 CPT에 게시 된 모든 제품을 나열하고이를 입력란과 함께 테이블 행에 넣습니다. 각 입력란에는 CPT 제품 ID를 기반으로 고유 한 ID가 있습니다.사용자 지정 메타 박스가 루프 입력 상자로 저장되지 않음
이 코드의 대부분은 다른 게시물을 기반으로 추가했지만 데이터를 저장하는 데 문제가 있습니다. 나는 그것이 나의 가치 = "", 나의 nonce 및 $ meta_value와 관련이 있다는 것을 알고 있지만 다음에 어디로 가야할지 모르겠습니다.
<?php
add_action('admin_init', 'my_theme_on_admin_init');
function my_theme_on_admin_init() {
add_meta_box('my_metabox',
__('Daily Inventory Levels', 'textdomain'),
'my_metabox_render',
'location_inventory', 'normal', 'high'
);
}
function my_metabox_render($post) {
// using an underscore, prevents the meta variable
// from showing up in the custom fields section
global $woocommerce, $post;
$productsList = new WP_Query('post_type=product&post_status=publish');
?>
<div class="inside">
<table class="form-table">
<input type="hidden" name="inventory_noncename" id="inventory_noncename" value="<?php wp_create_nonce('inventory-nonce'); ?>" />
<?php
while ($productsList->have_posts()) {
$productsList->the_post();
?>
<tr><td><label for="location_inventory_product-<?php the_ID(); ?>"><?php the_title(); ?></label></td><td><input id="location_inventory_product-<?php the_ID(); ?>" name="location_inventory_product-<?php the_ID(); ?>" cols="40" rows="5" value="<?php echo $meta_key; ?>" /></td></tr>
<?php }
wp_reset_postdata();
?>
</table>
</div>
<?php
}
/*update on save*/
add_action('save_post', 'save_postdata_dynamic_inventory_metabox');
function save_postdata_dynamic_inventory_metabox($post_id) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Check permissions
if ('location_inventory' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
}
elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if (isset($_POST['inventory_noncename'])){
if (!wp_verify_nonce($_POST['inventory_noncename'], 'inventory-nonce'))
return;
} else {
return;
}
// Get the posted data and sanitize it for use as an HTML class.
$new_meta_value = (isset($_POST['location_inventory_product-'.the_ID().'']) ? sanitize_html_class($_POST['location_inventory_product-'.the_ID().'']) : '');
// Get the meta key.
$meta_key = 'location_inventory_product-'.the_ID().'';
// Get the meta value of the custom field key.
$meta_value = get_post_meta($post_id, $meta_key, true);
// If a new meta value was added and there was no previous value, add it.
if ($new_meta_value && '' == $meta_value)
add_post_meta($post_id, $meta_key, $new_meta_value, true);
// If the new meta value does not match the old value, update it.
elseif ($new_meta_value && $new_meta_value != $meta_value)
update_post_meta($post_id, $meta_key, $new_meta_value);
// If there is no new meta value but an old value exists, delete it.
elseif ('' == $new_meta_value && $meta_value)
delete_post_meta($post_id, $meta_key, $meta_value);
} // ends function save_postdata_dynamic_reviews_metabox