Save custom dropdown field.
-
Hey there!!
Well, I have a little problem with my custom meta box. I create a custom post which includes a custom meta field dropdown. The dropdown displays the title of the posts from another custom posts that I want to link with when save. But when I clic on publish the dropdown doesn′t save.
Here is my code.
Here I create my custom post:
function my_custom_post_category_product() { $labels = array( 'name' => _x( 'Categories Products', 'post type general name' ), 'singular_name' => _x( 'Categories Products', 'post type singular name' ), 'add_new' => _x( 'Add New' ), 'add_new_item' => __( 'Add New Categories Products' ), 'edit_item' => __( 'Edit Categories Products' ), 'new_item' => __( 'New Categories Products' ), 'all_items' => __( 'All Categories Products' ), 'view_item' => __( 'View Categories Products' ), 'search_items' => __( 'Search Categories Products' ), 'not_found_in_trash' => __( 'No Categories Products found in the Trash' ), 'not_found' => __( 'No Categories Products found' ), 'parent_item_colon' => '', 'menu_name' => 'Categoria Producto' ); $args = array( 'labels' => $labels, 'description' => 'Holds our categories specific data', 'public' => true, 'menu_position' => 100, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ), 'has_archive' => true, 'taxonomies' => array('category'), ); register_post_type( 'category_product', $args ); } add_action( 'init', 'my_custom_post_category_product' ); function my_updated_messages_category_product( $messages ) { global $post, $post_ID; $messages['categories'] = array( 0 => '', 1 => sprintf( __('Categories Products updated. <a href="%s">View Categories Products</a>'), esc_url( get_permalink($post_ID) ) ), 2 => __('Custom field updated.'), 3 => __('Custom field deleted.'), 4 => __('Categories updated.'), 5 => isset($_GET['revision']) ? sprintf( __('Categories Products restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => sprintf( __('Categories Products published. <a href="%s">View Categories Products</a>'), esc_url( get_permalink($post_ID) ) ), 7 => __('Categories Products saved.'), 8 => sprintf( __('Categories Products submitted. <a target="_blank" href="%s">Preview Categories Products</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), 9 => sprintf( __('Categories Products scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Categories Products</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ), 10 => sprintf( __('Categories Products draft updated. <a target="_blank" href="%s">Preview Categories Products</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), ); return $messages; } add_filter( 'post_updated_messages', 'my_updated_messages_category_product' );
And then the function to create the custom meta box and save the data:
add_action('add_meta_boxes', 'product_post_options_box',10,2); /* saved the data */ add_action( 'save_post', 'save_product_post_options_box' ); function product_post_options_box($post_type, $post) { if ('category_product' == $post_type) //only add to your post type add_meta_box('post_info', 'Product Information', 'product_post_info', 'category_product', 'side', 'high'); } function product_post_info() { global $post; /* List the posts*/ $posts = get_posts( array( 'post_type' => array( 'categories' ), 'posts_per_page' => -1 ) ); //get saved data $saved = get_post_meta($post->ID,'_categories',true); if (empty($saved)) $saved = array(); //var_dump($saved); echo '<select name="categories" id="categories" 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__ ), 'categories_nonce' ); } function save_product_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['categories_nonce'] ) || ! wp_verify_nonce( $_POST['categories_nonce'], plugin_basename( __FILE__ ) ) ) return; // Thirdly check the post type if ('categories' != get_post_type($post_ID)) return; $mydata = $_POST['categories']; // Do something with $mydata // either using update_post_meta( $post_ID, '_category_product', $mydata, true); }
At this point:
//get saved data $saved = get_post_meta($post->ID,'_categories',true); if (empty($saved)) $saved = array(); //var_dump($saved);
If I show $saved in order to see what it contains it shows the custom posts from WordPress. I have no idea where is the bug. Any idea?
Note: The dropdown must save multiple options (1 post or more)
Thanks a lot!
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Save custom dropdown field.’ is closed to new replies.