0
votes

I'm creating a metabox with a checkbox group and i want to populate the checkboxes with a custom post type.

I'm using the code in this article: Reusable Custom Meta Boxes Part 2

Now for checkboxes group i have the values hardcoded:

    array (  
    'label' => 'Sponsors',  
    'desc'  => 'Sponsors for this exercise.',  
    'id'    => $prefix.'sponsors',  
    'type'  => 'checkbox_group',  
    'options' => array (  
        'one' => array (  
            'label' => 'Option One',  
            'value' => 'one'  
        ),  
        'two' => array (  
            'label' => 'Option Two',  
            'value' => 'two'  
        ),  
        'three' => array (  
            'label' => 'Option Three',  
            'value' => 'three'  
        ),  
        'four' => array (  
            'label' => 'Option Four',  
            'value' => 'four'  
        )  
    )  
)     

and this code saves the data:

case 'checkbox_group':  
foreach ($field['options'] as $option) {  
    echo '<input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta && in_array($option['value'], $meta) ? ' checked="checked"' : '',' /> 
            <label for="'.$option['value'].'">'.$option['label'].'</label><br />';  
}  

break;

I want to use a custom post type to load the checkboxes:

function get_sponsors()
{
    $args = array(
    'numberposts' = -1,
    'post_type' = 'px_sponsor',
    'orderby' = 'post_title',
    'order' = 'ASC'
    );

    $my_sponsors = get_posts( $args );

    return $my_sponsors;
}

How can i modify it so it shows my custom post type values instead of the hard coded ones?

1

1 Answers

0
votes

Solved!

i changed the function get_sponsors() to return an array with posts:

function get_sponsors()
{
    $args = array(
    'numberposts' => -1,
    'post_type' => 'movies'
    );

    $my_movies = get_posts($args);

    $options = array();
    foreach($my_movies as $m)
        $options[$m->ID] = array(
            'label' => $m->post_title,
            'value' => $m->ID
        );

    return $options;
}

Credits for solution to Mridul Aggarwal for replying in this post