Skip to content

Instantly share code, notes, and snippets.

@kjtolsma
Created May 17, 2017 11:41
Show Gist options
  • Save kjtolsma/a10299bffe7405446bbf63bfb481f326 to your computer and use it in GitHub Desktop.
Save kjtolsma/a10299bffe7405446bbf63bfb481f326 to your computer and use it in GitHub Desktop.
/* Multiple select */
function prefix_settings_multiple_select( $args ) {
$selected_terms = get_option( $args['label_for'] );
$selected_terms = is_array( $selected_terms ) ? $selected_terms : array();
$selected_terms = array_map( 'absint', $selected_terms );
printf(
'<select name="%s" id="%s" multiple>',
esc_attr( $args['label_for'] . '[]' ),
esc_attr( $args['label_for'] )
);
$query = new WP_Query( array(
'post_type' => 'some_post_type',
'posts_per_page' => 50,
) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
printf(
'<option value="%s" %s>%s</option>',
esc_attr( get_the_ID() ),
selected( in_array( get_the_ID(), $selected_terms, true ), true, false ),
esc_html( get_the_title() )
);
}
wp_reset_postdata();
}
printf( '</select>' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment