将add_meta_box数据保存到多个post_types

时间:2015-03-30 00:43:53

标签: wordpress

我有以下代码在我的Wordpress网站中创建和保存自定义元框。此解决方案适用于我的'work'post_type,但不会保存在标准的'post'上。两个post_types上的元框都显示正常,但只会节省“工作”。因此我认为问题在于保存功能,但我无法解释为什么只能在一个post_type上工作?

我已将下面的代码缩减到我认为必不可少的代码。

感谢。

// 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

1 个答案:

答案 0 :(得分:0)

经过几天的实验,我对我遇到的问题有一个半解决方案。在原始代码中,我试图注册三个元框,但是将其中两个放在多个帖子类型($ post_type)中,另一个只放在一个帖子类型(工作)中。我还没有确定原因,但这是导致元数据不能保存在其中一种帖子类型中的原因。

虽然这不是详细分类的完整答案,但它至少可以找到问题的根源,而我最初认为问题在于保存功能是正确的,我只是不知道如何解决这个问题。这个解决方案对我来说很好,但是我不再投入这个问题。

希望这对其他人至少可以作为一个起点。

// 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');
    }
    }