0

My custom Meta Box has a drop down field, but how do I set the default Option Value to be blank? It is currently populating via the CPT's meta_value, but I'd like that to change.

Here's a screenshot of the issue: enter image description here

I want the first option for the Second Author field to be blank/have no selection.

Code for Meta Box:

            case 'select' :
                $author_select_query = new WP_Query(array(
                    'post_type' => 'people',
                    'tax_query' => array(
                        'relation' => 'OR',
                        array(
                            'taxonomy' => 'role',
                            'field' => 'slug',
                            'terms' => array('adjunct-faculty'),
                        ),
                        array(
                            'taxonomy' => 'role',
                            'field' => 'slug',
                            'terms' => array('faculty'),
                        )
                    ),
                    'meta_key' => 'ecpt_people_alpha',
                    'orderby' => 'meta_value',
                    'order' => 'ASC',
                    'posts_per_page' => '-1')); 
                $authors = $author_select_query ->get_posts();
                echo '<select name="', $field['id'], '" id="', $field['id'], '">';
                foreach($authors as $author) {
                    echo '<option value="' . $author->ID . '"', $meta == $author->ID ? ' selected="selected"' : '', '>', $author->post_title, '</option>';
                }
                echo '</select>';
1

Before the foreach loop:

foreach($authors as $author) {

Add this empty option element:

echo '<option value="0"></option>';

Hope it works! :)

  • And add selected="selected" to it – Gangesh Nov 3 '14 at 18:14
  • Those work great, but for some reason the Books are still being attached to the first author, even when the empty value is selected – timmyg Nov 3 '14 at 19:00
  • @timmyg so try with that <option name="just-an-option" value="just-an-option"></option> – SLH Nov 3 '14 at 21:42

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.