Skip to content

Instantly share code, notes, and snippets.

@mustardBees
Created February 9, 2015 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustardBees/78a091b441359bd86839 to your computer and use it in GitHub Desktop.
Save mustardBees/78a091b441359bd86839 to your computer and use it in GitHub Desktop.
Generic function to return an array of posts formatted for CMB2. Simply pass in your WP_Query arguments and get back a beautifully formatted CMB2 options array.
<?php
/**
* Get a list of posts
*
* Generic function to return an array of posts formatted for CMB2. Simply pass
* in your WP_Query arguments and get back a beautifully formatted CMB2 options
* array.
*
* @param array $query_args WP_Query arguments
* @return array CMB2 options array
*/
function iweb_get_cmb_options_array( $query_args ) {
$defaults = array(
'posts_per_page' => -1
);
$args = wp_parse_args( $query_args, $defaults );
$query = new WP_Query( $args );
$posts_array = array();
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$posts_array[get_the_ID()] = get_the_title();
}
}
wp_reset_postdata();
return $posts_array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment