0

'm using human made's custom meta boxes framework (https://github.com/humanmade/Custom-Meta-Boxes). Using the post select:

array( 
    'id'       => 'example-field-post-select', 
    'name'     => 'Post Select Field', 
    'type'     => 'post_select', 
    'use_ajax' => true,
    'query' => array( 'post_type' => array('posttype') )
);

Currently, the way the post_select field works is that it populates with post ids in the select box for the name and value of the field. I'd like to populate the "name" part of the select box as the post type title and the "value" as the id.

Any idea how to accomplish this without modifying the field type or creating a custom select field?

2
  • Have you asked the author?
    – Milo
    Commented Sep 18, 2013 at 17:14
  • Searched the knowledge base and asked, usually find WA.SE answers quicker and clearer.
    – Rizzo
    Commented Sep 18, 2013 at 22:18

2 Answers 2

1

Unfortunately it is not possible to modify the field to use the name instead of ID and to keep the AJAX functionality.

You could use a standard select field, and pass the post data in the desired format to this - although you will loose the AJAX functionality.

The only way to achieve 100% of what you want is to create a custom CMB Field Type.

Unfortunately the Post_Select_Field and Select_Field classes are not the easiest to work with - the AJAX stuff in particular. I am currently working on a major overhaul of this and it should all be a bit clearer once this is released. Likeley to be a couple of weeks at least.

Update to include some code

This is how you would do it with a standard select field:

add_filter( 'cmb_meta_boxes', 'cmb_sample_metaboxes' );
function cmb_sample_metaboxes( array $meta_boxes ) {

    $post_select_options = array();

    $my_query = new WP_Query( 'post_type=post&posts_per_page=100' );

    while ( $my_query->have_posts() ) {
        $my_query->the_post();
        global $post;
        $post_select_options[$post->post_name] = get_the_title( get_the_id() );
    }

    wp_reset_postdata();

    $meta_boxes[] = array(
        'title' => 'CMB Test - all fields',
        'pages' => 'post',
        'fields' => array(
            array( 'id' => 'field-ID', 'name' => 'Post Select field', 'type' => 'select', 'options' => $post_select_options, 'allow_none' => true ),        
        )
    );

    return $meta_boxes;

}
0

So, this was made more possible with Jupiter Cow's modification of the Update Post plugin, found here:

https://bitbucket.org/jupitercow/gravity-forms-update-post/src

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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