Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am working on meta fields for a custom post type storing match data. I have two sepearte meta boxes one which the team and the score are chosen and the values are submitted.

The second takes that information and provides a series of select boxes in which players from the team in question are displayed. As there are several goals scored, several players are chosen (some more than once)

I am working off a tutorial http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-3-extra-fields/ and am modifying the code to use repeat select boxes. This is what I have come up with:

$goal_info_meta_fields = array(
    'label' => 'Home Goals by:',
    'desc' => 'List home scorers',
    'id' => $prefix.'home-scorers',
    'type' => 'home_player_list'
);

function show_goal_info_meta_box() {
global $goal_info_meta_fields, $post;


echo '<input type="hidden" name="goal_info_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';


echo '<table class="admin-table">';
foreach ($goal_info_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']) {

        // home_player_list
    case 'home_player_list':
        echo '<a class="repeatable-add button" href="#">+</a>
            <ul id="'.$field['id'].'-repeatable" class="custom_repeatable">';
        $post_id = get_the_ID();
        $team_id = get_post_meta( $post_id, 'report_home-select');

        $items = get_users( array (
                'meta_key' => 'team-meta',
                'meta_value' => $team_id
            ));
        $i = 0;
        if ($meta) {
            foreach($meta as $row) {
                echo '<li><span class="sort hndle button">|||</span>';
                echo '<select name="'.$field['id'].'['.$i.']" class="chzn-select" style="width:200px;" id="report_home-scorers">
                <option value="">Select One</option>'; // Select One
                foreach($items as $item) {
                    echo '<option value="'.$item->ID.'"',$meta == $item->ID ? ' selected="selected"' : '','>'.$item->display_name.'</option>';
                }
                echo '</select><a class="repeatable-remove button" href="#">-</a></li>';

                $i++;
            }
        } else {
            echo '<li><span class="sort hndle button">|||</span>';
            echo '<select name="'.$field['id'].'['.$i.']" class="chzn-select" style="width:200px;" id="'.$field['id'].'">
            <option value="">Select One</option>'; // Select One

            foreach ($items as $item) {
                echo '<option value="'.$item->ID.'"',$meta == $item->ID ? ' selected="selected"' : '','>'.$item->display_name.'</option>';
            }

            echo '</select><a class="repeatable-remove button" href="#">-</a></li>';

        }
        echo '</ul><span class="description">'.$field['desc'].'</span>';
        break;


    } //end switch
    echo '</td></tr>';
} // end foreach
echo '</table>'; // end table

}

It works perfectly if I use text imputs but not the select boxes and I think the problem is that $meta == $item->ID ? ' selected="selected"' : and currently $meta = a:1:{i:0;s:3:"179";} I've tried unserializing the array as well.

The tutorial I've used has a github repository https://github.com/tammyhart/Reusable-Custom-WordPress-Meta-Boxes which I tried as well but I couldn't apply the repeatable example as the options are in a loop I can't use a foreach statement in an array.

I would love some help with this as I've been tearing my hair out for the past two days.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

To make this work...

$meta == $item->ID ? ' selected="selected"' : '';

... with an array like a:1:{i:0;s:3:"179";} which unserializes to...

Array
(
    [0] => 179
)

... you'd need to do this:

$meta =  unserialize($meta);
$meta[0] == $item->ID ? ' selected="selected"' : '';

But that line shows up inside a foreach like foreach($meta as $row) {, at least in one place, so I am wondering why it isn't written...

$row == $item->ID ? ' selected="selected"' : '';

And the second time that line-- $meta[0] == $item->ID ? ' selected="selected"' : '' -- shows up is inside an else that should not execute unless $meta is empty, so it shouldn't using the variable at all.

I'd suggest that you enable debugging and take a good look around.

share|improve this answer
    
Thank you so much for your response though I'm not entirely sure I understood it correctly. So in the first one where $meta has data I do `$row == $item->ID ? ' selected="selected"' : and the second isn't needed anyway? I do have debugging enabled but I only get errors before I submit the team name data. –  mantis Jun 12 '13 at 22:42
    
I think so, yes. It looks to me like $row should be your ID and inside that else $meta isn't populated so there is no point in checking it. –  s_ha_dum Jun 12 '13 at 22:47
    
still returning a single value inside an array. I've tried foreach($meta[$i] as $row) {, but still no luck. –  mantis Jun 13 '13 at 11:09
    
Got it! You were dead on with $row == $item->ID ? selected="selected"' : I made a mistake cloning the box and cloned the [0]as well. Thank you again! I would give you a +1 but my rep isn't good enough. –  mantis Jun 13 '13 at 13:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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