0

我正在创建一个循环遍历另一个 CPT 的所有帖子的元框:“产品”。元框列出了该 CPT 中所有已发布的产品,并将它们与输入框一起放在表格行中。每个输入框都有一个基于 CPT 产品 ID 的唯一 ID。

我根据其他帖子添加的大部分代码,但我在保存数据时遇到问题。我知道这与我的 value=""、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
4

1 回答 1

0

一些观察:

1)钩子save_post有2个参数:

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

function save_postdata_dynamic_inventory_metabox( $post_id, $post_object ) { }

2)我认为没有必要检查权限(至少,从未见过)。以防万一,这是一个post_object使用示例:

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

3) 缩短此检查:

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

4) the_ID()inside 函数的使用save_postdata_dynamic_inventory_metabox是错误的。这仅在循环内可用,并且您已经$post_id在手边。

5)最后,也是最重要的,你处理的方式$meta_key是错误的。一方面,它没有在函数内部定义my_metabox_render。检查此答案以获取工作示例。

研究此搜索查询以找到更多示例。

于 2013-08-27T19:01:57.097 回答