Populate Select List with Custom Post Type Name

Support General Populate Select List with Custom Post Type Name

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #6922
    tonyrboiestonyrboies
    Participant

    Pretty common request, have reviewed a lot of documentation but still can't nail it down. My custom post type is "staffmembers" -- I think this is in the ballpark, but was wondering what I have wrong. Thanks in advance.

    $options = array();
    $post_types = get_post_types( '', 'staffmembers', 'objects' );
    
    foreach ( $post_types as $post_type ) {
        $options[$post_type->slug] = $post_type->name;
    }
    
    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array (
          'id' => 'author',
          'title' => 'Author',
          'pages' =>   array (
             'post',
          ),
          'context' => 'normal',
          'priority' => 'high',
          'autosave' => false,
          'fields' =>   array (
             
            array (
              'id' => 'select_2',
              'name' => 'Select Field',
              'type' => 'select',
              'placeholder' => 'Select an Item',
              'options' =>       $options,
            ),
          ),
          '' => '',
        );
    
        return $meta_boxes;
    }
    #6924
    Truong GiangTruong Giang
    Participant

    Hi there,

    There are some problems:

    1. get_post_types() seems to be called wrongly, read the documentation here: https://developer.wordpress.org/reference/functions/get_post_types/

    2. You should move the code handle $options to inside your_prefix_register_meta_boxes() function

    3. Do you want to get all post types or all posts in a post type?

    #6939
    tonyrboiestonyrboies
    Participant

    Thanks Truong, yes, I just want to pull posts from a single custom post type "staffmembers" and the code I have I think is more for pulling from any post type.

    So change this

    function your_prefix_register_meta_boxes( $meta_boxes ) {

    to this

    function your_prefix_register_meta_boxes( $meta_boxes, $options ) {

    Stuck on replacing $post_types = get_post_types( '', 'staffmembers', 'objects' ); even after reading the documentation, though. Goal is a dropdown list of the slugs of the custom post type "staffmembers" as the option value, and if possible the post name as the visible option name. If they need to be the same, that would work for the purposes of the list.

    #6941
    tonyrboiestonyrboies
    Participant

    Okay, I was way offtrack. Have selected POST and now the select list is populating from the desired custom post type. I selected field type "Select Advanced" but how do I get that list to support multiple selections?

    #6942
    tonyrboiestonyrboies
    Participant

    Got the "select multiple" via the custom attributes. Am good to go. Btw, like the plugin a lot.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Populate Select List with Custom Post Type Name’ is closed to new replies.