Created
February 9, 2015 11:20
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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