Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example code to populate a CMB select field with posts from a custom post type.
<?php
/**
* Get a list of downloads
*
* Return an array of downloads suitable for CMB's select field.
*
* Example CMB select field usage:
*
* array(
* 'name' => 'Download',
* 'id' => $prefix . 'download',
* 'type' => 'select',
* 'options' => pw_get_downloads()
* ),
*
* @author Phil Wylie
* @link http://goo.gl/c0bPb
* @return array $downloads_array Array containing titles and post IDs
*/
function pw_get_downloads() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'download',
'orderby' => 'title',
'order' => 'ASC'
);
$downloads = new WP_Query( $args );
$downloads_array = array();
if ( $downloads->have_posts() ) {
while ( $downloads->have_posts() ) {
$downloads->the_post();
$downloads_array[] = array(
'name' => get_the_title(),
'value' => get_the_ID()
);
}
}
wp_reset_postdata();
return $downloads_array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.