0

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?

1

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;
}
  • 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
0
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
)
  • 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 – codepixlabs Aug 28 '13 at 4:02

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.