Skip to content

Instantly share code, notes, and snippets.

@mustardBees
Last active December 17, 2015 18:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mustardBees/5650744 to your computer and use it in GitHub Desktop.
Save mustardBees/5650744 to your computer and use it in GitHub Desktop.
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