0

I am using this code to show post type dropdown to select available post types but when i submit the post it restores the value back to post, how can i make sure it saves it?

<select name='my_meta_box_post_type' id='my_meta_box_post_type'>
        <?php $post_types=get_post_types('', 'objects'); foreach ($post_types as $post_type): ?>
        <option value="<?php echo esc_attr($post_type->name); ?>"><?php echo esc_html($post_type->name); ?></option>
        <?php endforeach; ?>
    </select>
1
  • Where is this select box located? what you are doing to save it's value ? it won't save just by selecting value. Apr 1, 2015 at 9:42

1 Answer 1

1

I recommend not reinventing the wheel and looking at Reusable-Custom-WordPress-Meta-Boxes

Even though this code is no longer supported it's concise enough to be easily manageable. I use it and it works well.

All you need to do is add the meta_box.php file to your functions.php and from then on use the predefined components:

require_once('path_to/metaboxes/meta_box.php');

Then use code such as (this is the example from the github page):

$prefix = 'sample_';

$fields = array(
    array( // Text Input
    'label' => 'Text Input', // <label>
    'desc'  => 'A description for the field.', // description
    'id'    => $prefix.'text', // field id and name
    'type'  => 'text' // type of field
),
array( // Textarea
    'label' => 'Textarea', // <label>
    'desc'  => 'A description for the field.', // description
    'id'    => $prefix.'textarea', // field id and name
    'type'  => 'textarea' // type of field
)
);

$sample_box = new custom_add_meta_box( 'sample_box', 'Sample Box', $fields, 'post', true );

There are also other options such as CMB2 (Custom Meta Boxes) That code appears to have better support but is also much more complex and could be overkill for your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.