Wordpress 将添加元数据框数据保存到多个post类型

Wordpress 将添加元数据框数据保存到多个post类型,wordpress,Wordpress,我有以下代码在我的Wordpress站点中创建和保存自定义元框。此解决方案适用于我的“工作”帖子类型,但不会保存在标准的“帖子”上。元框在两种post_类型上都显示良好,但仅在“工作”上保存。因此,我认为问题在于save函数,但我无法解释为什么它只适用于一种post_类型 我已经将下面的代码缩减到我认为是最基本的代码 谢谢 // Allow registering of custom meta boxes add_action( 'add_meta_boxes', 'add_custom_met

我有以下代码在我的Wordpress站点中创建和保存自定义元框。此解决方案适用于我的“工作”帖子类型,但不会保存在标准的“帖子”上。元框在两种post_类型上都显示良好,但仅在“工作”上保存。因此,我认为问题在于save函数,但我无法解释为什么它只适用于一种post_类型

我已经将下面的代码缩减到我认为是最基本的代码

谢谢

// Allow registering of custom meta boxes
add_action( 'add_meta_boxes', 'add_custom_metaboxes' );

// Register the Project Meta Boxes
function add_custom_metaboxes()  {
    $post_types = array ( 'post', 'work' );
    foreach( $post_types as $post_type )
    {
    add_meta_box('xx_introduction', 'Introduction', 'xx_introduction', $post_type, 'normal', 'high');
    add_meta_box('xx_more', 'More', 'xx_more', 'work', 'normal', 'high');
}
}

    // Add Introduction Metabox
    function xx_introduction() {
        global $post;

        // Noncename needed to verify where the data originated
        echo '<input type="hidden" name="introductionmeta_noncename" id="introductionmeta_noncename" value="' . 
        wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

        // Get the introduction data if its already been entered
        $introduction = get_post_meta($post->ID, '_introduction', true);

        // Echo out the field
        echo '<textarea name="_introduction" class="widefat" rows="5">' . $introduction  . '</textarea>';

    }

    // Add More Metabox
    function xx_more() {
        global $post;

        // Noncename needed to verify where the data originated
        echo '<input type="hidden" name="moremeta_noncename" id="moremeta_noncename" value="' . 
        wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

        // Get the more data if its already been entered
        $more = get_post_meta($post->ID, '_more', true);

        // Echo out the field
        echo '<textarea name="_more" class="widefat" rows="5">' . $more  . '</textarea>';

    }

    // Save the Metabox Data
    function xx_save_custom_meta($post_id, $post) {

        // verify this came from the our screen and with proper authorization,
        // because save_post can be triggered at other times
        if ( !wp_verify_nonce( $_POST['introductionmeta_noncename'], plugin_basename(__FILE__) )) {
        return $post->ID;
        }
        }

        // Is the user allowed to edit the post or page?
        if ( !current_user_can( 'edit_post', $post->ID ))
            return $post->ID;

        // OK, we're authenticated: we need to find and save the data
        // We'll put it into an array to make it easier to loop though.

        $introduction_meta['_introduction'] = $_POST['_introduction'];
        $more_meta['_more'] = $_POST['_more'];

        // Add values of $introduction_meta as custom fields
        foreach ($introduction_meta as $key => $value) { // Cycle through the $testimonial_meta array
            if( $post->post_type == 'revision' ) return; // Don't store custom data twice
            $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
            if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
                update_post_meta($post->ID, $key, $value);
            } else { // If the custom field doesn't have a value
                add_post_meta($post->ID, $key, $value);
            }
            if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
        }
    }

    // Add values of $more_meta as custom fields
        foreach ($more_meta as $key => $value) { // Cycle through the $more_meta array
            if( $post->post_type == 'revision' ) return; // Don't store custom data twice
            $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
            if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
                update_post_meta($post->ID, $key, $value);
            } else { // If the custom field doesn't have a value
                add_post_meta($post->ID, $key, $value);
            }
            if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
        }
    }

add_action('save_post', 'xx_save_custom_meta', 1, 2); // save the custom fields

经过几天的试验,我对我所经历的问题有了一个半解决方案。在最初的代码中,我试图注册三个元框,但将其中两个放在多个post类型$post_type中,另一个放在一个post类型中。我还没有确定原因,但这就是导致元数据不能保存在某个post类型中的原因

虽然这不是一个完整的答案和详细的细分,但它至少找到了问题的根源,而且我最初认为问题存在于save函数中是正确的,我只是不知道如何解决这个问题。这个解决方案对我来说很好,所以我不会再在这个问题上投入更多的时间

希望这对其他人有用,至少作为一个起点

// Register the Project Meta Boxes
    function add_custom_metaboxes()  {
        $post_types = array ( 'post', 'work' );
        foreach( $post_types as $post_type )
        {
        add_meta_box('xx_introduction', 'Introduction', 'xx_introduction', $post_type, 'normal', 'high');
        add_meta_box('xx_more', 'Introduction', 'xx_introduction', $post_type, 'normal', 'high');
    }
    }