0

I have a custom meta box. In it there are input fields, as well as a select box. What I want, is to have the get_tags array list to be outputted as the values in the select box.

I currently have the select box option values hard coded. I want this to be dynamic depending on the tags on the website.

Here is the code;

    // Add the Meta Box
function spk_project_info_meta_box() {
    add_meta_box(
        'spk_project_info_meta_box', // $id
        'Project Information', // $title 
        'show_spk_project_info_meta_box', // $callback
        'post', // $page
        'normal', // $context
        'default'); // $priority
}
add_action('add_meta_boxes', 'spk_project_info_meta_box');


// Field Array
$prefix = 'spk_project_info_';
$tags_array = get_tags();
$custom_meta_fields = array(
    array(
        'label'=> 'Client',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'client',
        'type'  => 'text'
    ),
    array(
        'label'=> 'Our Role',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'role',
        'type'  => 'text'
    ),
    array(
        'label'=> 'Year',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'year',
        'type'  => 'text'
    ),

    array(
        'label'=> 'Select Box',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'select',
        'type'  => 'select',
        'options' => array (  
        'one' => array (
            'label' => 'Option One',
            'value' => 'one'
        ),
        'two' => array (
            'label' => 'Option Two',
            'value' => 'two'
        ),
        'three' => array (
            'label' => 'Option Three',
            'value' => 'three'
        )
      )
    )
);

// The Callback
function show_spk_project_info_meta_box() {
global $custom_meta_fields, $post;
echo '<input type="hidden" name="spk_project_info_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
    echo '<table class="form-table">';
    foreach ($custom_meta_fields as $field) {
        $meta = get_post_meta($post->ID, $field['id'], true);
        echo '<tr>
                <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
                <td>';
                switch($field['type']) {
                    case 'text':
                        echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
                            <br /><span class="description">'.$field['desc'].'</span>';
                    break;

                    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;
                }
        echo '</td></tr>';
    } // end foreach
    echo '</table>'; // end table
}

function save_spk_project_info_meta_data($post_id) {
    global $custom_meta_fields;
    if ( !wp_verify_nonce($_POST['spk_project_info_meta_box_nonce'], basename(__FILE__) ) ) 
        return $post_id;
    if ( defined('DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;
    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;
    }
    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_spk_project_info_meta_data');
0

You can do it like so — replace the $tags_array = get_tags(); with this:

$tags_opt = array();
foreach ( get_tags() as $term ) {
    $tags_opt[] = array(
        'label' => $term->name,
        'value' => $term->term_id,
    );
}

And then, for the "Select Box" field, set the options to $tags_opt like this:

array(
    'label' => 'Select Box',
    ...
    'options' => $tags_opt,
)

If you want to save the term/tag slug instead of its ID (term_id), change the 'value' => $term->term_id to 'value' => $term->slug.

And if you want to include tags without any posts, use get_tags( array( 'hide_empty' => 0 ) ).

  • Thank you for the above. This is correct and it works perfectly. I wish I could give you vote, but I have a low reputation. – Leroy Nov 7 '18 at 17:51
  • You can accept the answer. We both gain some rep, and people will also know that the question has been answered. :) – Sally CJ Nov 7 '18 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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