Skip to content

Instantly share code, notes, and snippets.

@alordiel
Last active June 6, 2017 12:02
Show Gist options
  • Save alordiel/55e6a30b15fe78d36f75fa08b8dcacf4 to your computer and use it in GitHub Desktop.
Save alordiel/55e6a30b15fe78d36f75fa08b8dcacf4 to your computer and use it in GitHub Desktop.
WP PHP: build an array of post id => post title pairs by CPT
/**
* Function to query a given post type and to build array of the avialable titles. Used for select lists
* Used by:
*
* @param string $post_type Post type
* @return array $pairs Associative array with key[post id] and value[post title]
*/
function get_post_type_list_of_posts($post_type){
$pairs = array();
$post_data = get_posts( array(
'post_type' => $post_type,
'posts_per_page' => -1,
'orderby' => 'post_title',
'post_status' => array('publish','private')
));
$pairs["none"] = '---';
if($post_data){
foreach ($post_data as $data) {
$pairs[$data->ID] = $data->post_title;
}
}
return $pairs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment