2013-08-27 2 views
0

다른 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 

답변

0

일부 관찰 :

add_action('save_post', 'save_postdata_dynamic_inventory_metabox', 10, 2); 

function save_postdata_dynamic_inventory_metabox($post_id, $post_object) { } 

2)가 권한을 확인 생각하지 않는다 :

1) 후크 save_post 2 개 인수를을 아래 코드는 내가 지금까지 가지고있다 필요한 (적어도, 본 적이 없다).

if ('location_inventory' == $post_object->post_type) 

3) 짧은이 체크 :

if (!isset($_POST['inventory_noncename']) || !wp_verify_nonce($_POST['inventory_noncename'], 'inventory-nonce')) 
    return; 

4) save_postdata_dynamic_inventory_metabox 잘못 함수 내부 the_ID()의 사용은 이런 경우에, 이것은 post_object 사용의 예입니다. 이 기능은 루프 내에서만 사용할 수 있으며 이미 $post_id을 보유하고 있습니다.

5) 마지막으로 가장 중요한 것은 $meta_key을 처리하는 방식이 잘못되었습니다. 하나는 함수 my_metabox_render 안에 정의되어 있지 않습니다. 작업 예제는 this Answer을 확인하십시오.

연구에서 더 많은 예제를 찾으려면 this search query을 방문하십시오.

관련 문제