Add to Select Box with PHP Loop

Support General Add to Select Box with PHP Loop

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #6141
    birdsongcreativebirdsongcreative
    Participant

    Can anyone help me figure out how to loop through some other custom post types and populate a select dropdown with values?

    For example - if I wanted to associate Products with another post type by id - I would want to populate this with a custom loop.. is this even possible?

    array(
    'name'     => __( 'Select', 'rwmb' ),
    'id'       => "{$prefix}select",
    'type'     => 'select',
    // Array of 'value' => 'Label' pairs for select box
    'options'  => array(
    	'value1' => __( 'Label1', 'rwmb' ),
    	'value2' => __( 'Label2', 'rwmb' ),
    ),
    // Select multiple values, optional. Default is false.
    'multiple'    => false,
    'std'         => 'value2',
    'placeholder' => __( 'Select an Item', 'rwmb' ),
    ),
    #6144
    Anh TranAnh Tran
    Keymaster

    Hello,

    Do you want to get list of custom post types or list of posts of some custom post types?

    In 1st case, you can use the function get_post_types() to get all the post types:

    $options = array();
    $post_types = get_post_types( array(
        'public'   => true,
        '_builtin' => false
    ), 'objects' );
    foreach ( $post_types as $post_type ) {
        $options[$post_type->slug] = $post_type->name;
    }

    And in the select, just set 'options' => $options.

    In the 2nd case, you can use the field type post and set 'post_type' => 'your_post_type', and all the posts will be populated in the select option. For more info, please check this guide.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The topic ‘Add to Select Box with PHP Loop’ is closed to new replies.