Speichern Sie benutzerdefinierte Felder

Ich arbeite in einem neuen Projekt mit WordPress. Ich habe einige benutzerdefinierte Posts erstellt und jetzt muss ich einige benutzerdefinierte Posts wie Kategorien beziehen.

Ich habe ein benutzerdefiniertes Dropdown-Menü in den benutzerdefinierten Posts erstellt. In der Dropdown-Liste wird der Titel der Zollstelle angezeigt, aber ich weiß nicht, wie ich sie speichern soll.

Hier ist mein Code:

/* 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 a post'; while ( have_posts() ) : the_post(); echo ''; the_title(); echo ''; endwhile; echo ''; } 

Ich möchte die function save_post verwenden, weiß aber nicht wie. Natürlich muss das Dropdown-Menü eine Mehrfachauswahl ermöglichen.

Solutions Collecting From Web of "Speichern Sie benutzerdefinierte Felder"

Ich habe das Problem mit diesem Code getriggers, indem ich wieder de Meta-Box erstellt habe:

 // 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 ''; // Begin the field table and loop echo ''; 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 ''; } // end foreach echo '
'; switch($field['type']) { // case items will go here // select case 'select': echo '
'.$field['desc'].''; break; // checkbox case 'checkbox': echo ' '; break; // post_list case 'post_list': $items = get_posts( array ( 'post_type' => $field['post_type'], 'posts_per_page' => -1 )); echo '
'.$field['desc'].''; break; } //end switch echo '
'; // 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');``

eine Sache bevor ich beginne: in diesem Zusammenhang query_posts zu verwenden ist einfach schlecht schlecht. Im Übrigen ist hier ein Beispiel dafür, wie Sie die Metabox mit den Daten erstellen und speichern können und eine kleine Nonce-validation verwenden können.

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