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'm using the CMB framework for creating metaboxes (https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress)

and I want to add a select box which prepopulates with titles from a custom post type. A normal select box looks like:

array(
    'name' => 'Test Select',
    'desc' => 'field description (optional)',
    'id' => $prefix . 'test_select',
    'type' => 'select',
    'options' => array(
        array('name' => 'Option One', 'value' => 'standard'),
        array('name' => 'Option Two', 'value' => 'custom'),
        array('name' => 'Option Three', 'value' => 'none')              
    )
),

I want to do something like:

array(
    'name' => 'Test Select',
    'desc' => 'field description (optional)',
    'id' => $prefix . 'test_select',
    'type' => 'select',
    'options' => array(
        query_posts( array( 'post_type' => 'myposttype' ) )
),

where the select options are populated from the titles of a custom post type.. but it's not working the way I though. Any Ideas?

share|improve this question

2 Answers 2

query_posts( array( 'post_type' => 'myposttype' );

while ( have_posts() ) : the_post();

    $title_list[] =  array('name' => the_title(), 'value' => the_title());

endwhile;
wp_reset_query();

array(

    'name' => 'Test Select',
    'desc' => 'field description (optional)',
    'id' => $prefix . 'test_select',
    'type' => 'select',
    'options' => $title_list
)
share|improve this answer
    
this returns: Parse error: syntax error, unexpected T_WHILE –  Rizzo Aug 27 '13 at 14:45
    
There is a semicolon missing on the end of the first line. –  Charles Clarkson Aug 27 '13 at 16:21
    
check now i have corrected the parse error –  pr1nc3 Aug 28 '13 at 4:02

Use a function that returns an array of arrays that the options field expects.

Something like this (untested code):

array(
    'name' => 'Test Select',
    'desc' => 'field description (optional)',
    'id' => $prefix . 'test_select',
    'type' => 'select',
    'options' => get_myposttype_options('myposttype'),
),

function get_myposttype_options($argument) {

    $get_post_args = array(
        'post_type' => $argument,
    );

    $options = array();
    foreach ( get_posts( $get_post_args ) as $post ) {
        $title = get_the_title( $post->ID );

        $options[] = array(
            'name'  => $title,
            'value' => $title,
        );
    }

    return $options;
}
share|improve this answer
    
This looks great, should work.. but comes back blank –  Rizzo Aug 27 '13 at 14:46
    
Which part is blank? The whole page or just the return value from the function? –  Charles Clarkson Aug 27 '13 at 16:20
    
value from the function. not pulling and information. Would it be better to create a field type instead and reference it that way? github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress/… –  Rizzo Aug 27 '13 at 18:35
    
The case on id was wrong. I edited the answer with the change. –  Charles Clarkson Aug 28 '13 at 4:27

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.