Save Custom Fields

时间:2013-05-27 作者:Luismin

我正在使用WordPress的一个新项目中工作。我已经创建了一些自定义帖子,现在我必须关联一些自定义帖子,如类别。

我在自定义帖子中创建了一个自定义下拉菜单。下拉列表显示海关帖子的标题,但我不知道如何保存它们。

这是我的代码:

/* add custom field to my customo post */

add_action(\'admin_menu\', \'related_post_options_box\');

function related_post_options_box() {
add_meta_box(\'post_info\', \'Related Information\', \'related_post_info\', \'related\', \'side\', \'high\');
}

function related_post_info() {
global $post;
/* List the posts*/
    query_posts( array(\'post_type\' => array( \'related\', \'produktkrav\', \'filtreringskriterie\' )));
        echo \'<select name="related" id="related" multiple>\'.
         \'<option value="" selected="selected">Select a post</option>\';
        while ( have_posts() ) : the_post();
echo \'<option value="\';
        the_permalink();
        echo \'">\';
        the_title();
        echo \'</option>\';
    endwhile;

    echo \'</select>\';
}
我想使用save\\u post功能,但我不知道如何使用。当然,下拉列表必须允许多重选择。

2 个回复
最合适的回答,由SO网友:Luismin 整理而成

我通过再次创建de meta框解决了此代码的问题:

// Add the Meta Box
function add_custom_meta_box() {
    add_meta_box(
        \'custom_meta_box\', // $id
        \'Product Requirements\', // $title
        \'show_custom_meta_box\', // $callback
        \'produktkrav\', // $page
        \'normal\', // $context
        \'high\'); // $priority
}
add_action(\'add_meta_boxes\', \'add_custom_meta_box\');

    // Field Array
    $prefix = \'custom_\';
    $custom_meta_fields = array(  

        array(
            \'label\'=> \'Filter criteria\',
            \'desc\'  => \'Select the filter criteria for this product requirement.\',
            \'id\'    => $prefix.\'select\',
            \'type\'  => \'select\',
            \'options\' => array (
                \'one\' => array (
                    \'label\' => \'Miljo- og energikrav\',
                    \'value\' => \'Miljo- og energikrav\'
                ),
                \'two\' => array (
                    \'label\' => \'Tildelingskriterier\',
                    \'value\' => \'Tildelingskriterier\'
                ),
                \'three\' => array (
                    \'label\' => \'Leverandorens\',
                    \'value\' => \'Leverandorens\'
                )
            )
        ),
        array(
            \'label\'=> \'Mandatory Requirement\',
            \'desc\'  => \'Check if this product requirement is mandatory.\',
            \'id\'    => $prefix.\'checkbox\',
            \'type\'  => \'checkbox\'
        ),

        array(
            \'label\' => \'Categories\',
            \'desc\' => \'Related categorie.\',
            \'id\'    =>  $prefix.\'post_id\',
            \'type\' => \'post_list\',
            \'post_type\' => array(\'category_product\'),
)
    );  

    // The Callback
function show_custom_meta_box() {
global $custom_meta_fields, $post;
// Use nonce for verification
echo \'<input type="hidden" name="custom_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';  

    // Begin the field table and loop
    echo \'<table class="form-table">\';
    foreach ($custom_meta_fields as $field) {
        // get value of this field if it exists for this post
        $meta = get_post_meta($post->ID, $field[\'id\'], true);
        // begin a table row with
        echo \'<tr>
                <th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
                <td>\';
                switch($field[\'type\']) {
                    // case items will go here 

                    // select
case \'select\':
    echo \'<select name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'">\';
    foreach ($field[\'options\'] as $option) {
        echo \'<option\', $meta == $option[\'value\'] ? \' selected="selected"\' : \'\', \' value="\'.$option[\'value\'].\'">\'.$option[\'label\'].\'</option>\';
    }
    echo \'</select><br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;

// checkbox
case \'checkbox\':
    echo \'<input type="checkbox" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" \',$meta ? \' checked="checked"\' : \'\',\'/>
        <label for="\'.$field[\'id\'].\'">\'.$field[\'desc\'].\'</label>\';
break; 

// post_list
case \'post_list\':
$items = get_posts( array (
    \'post_type\' => $field[\'post_type\'],
    \'posts_per_page\' => -1
));
    echo \'<select name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'">
            <option value="">Select One</option>\'; // Select One
        foreach($items as $item) {
            echo \'<option value="\'.$item->ID.\'"\',$meta == $item->ID ? \' selected="selected"\' : \'\',\'> \'.$item->post_title.\'</option>\';
        } // end foreach
    echo \'</select><br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
                } //end switch
        echo \'</td></tr>\';
    } // end foreach
    echo \'</table>\'; // end table
}

// Save the Data
function save_custom_meta($post_id) {
    global $custom_meta_fields;  

    // verify nonce
    if (!wp_verify_nonce($_POST[\'custom_meta_box_nonce\'], basename(__FILE__)))
        return $post_id;
    // check autosave
    if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
        return $post_id;
    // check permissions
    if (\'page\' == $_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;
    }  

    // loop through fields and save the data
    foreach ($custom_meta_fields as $field) {
        $old = get_post_meta($post_id, $field[\'id\'], true);
        $new = $_POST[$field[\'id\']];
        if ($new && $new != $old) {
            update_post_meta($post_id, $field[\'id\'], $new);
        } elseif (\'\' == $new && $old) {
            delete_post_meta($post_id, $field[\'id\'], $old);
        }
    } // end foreach
}
add_action(\'save_post\', \'save_custom_meta\');``

SO网友:Bainternet

在我开始之前,有一件事是:在这种上下文中使用query\\u posts是很糟糕的。至于其余部分,这里是一个示例,说明如何使用创建元框并保存数据,以及使用一些次要的nonce验证。

/* use the right hook to  add meta box */
add_action(\'add_meta_boxes\', \'related_post_options_box\',10,2);
/* saved the data */
add_action( \'save_post\', \'save_related_post_options_box\' );

function related_post_options_box($post_type, $post) {
    if (\'related\' == $post_type) //only add to your post type
        add_meta_box(\'post_info\', \'Related Information\', \'related_post_info\', \'related\', \'side\', \'high\');
}

function related_post_info() {
    global $post;
    /* List the posts*/
    $posts = get_posts( array(
        \'post_type\'      => array( \'related\', \'produktkrav\', \'filtreringskriterie\' ),
        \'posts_per_page\' => -1
        )
    );
    //get saved data 
    $saved = get_post_meta($post->ID,\'_related\',true);
    if (empty($saved))
        $saved = array();

    echo \'<select name="related" id="related" multiple>\'.
        \'<option value="" selected="selected">Select a post</option>\';
    foreach ($posts as $p) {
        $link = get_permalink( $p->ID);
        $selected = (in_array($link,$saved)) ? \' selected="selected"\' : \'\';
        echo \'<option\'.$selected.\' value="\'.$link.\'">\'.get_the_title( $p->ID ).\'</option>\';
    }
    echo \'</select>\';

    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), \'related_nonce\' );

}

function save_related_post_options_box($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;

    // First we need to check if the current user is authorised to do this action. 
    if ( ! current_user_can( \'edit_post\', $post_ID ) )
        return;
    }

    // Secondly we need to check if the user intended to change this value.
    if ( ! isset( $_POST[\'related_nonce\'] ) || ! wp_verify_nonce( $_POST[\'related_nonce\'], plugin_basename( __FILE__ ) ) )
      return;

    // Thirdly check the post type
    if (\'related\' != get_post_type($post_ID))
        return;

    $mydata =  $_POST[\'related\'];

    // Do something with $mydata 
    // either using 
    update_post_meta( $post_ID, \'_related\', $mydata, true);

}

结束

相关推荐