Skip to content

Instantly share code, notes, and snippets.

@damianwajer
Created November 7, 2014 09:25
Show Gist options
  • Save damianwajer/23d2ee9a006b37fca17d to your computer and use it in GitHub Desktop.
Save damianwajer/23d2ee9a006b37fca17d to your computer and use it in GitHub Desktop.
[WordPress] Function to get an array with all posts from specific post type
<?php
/**
* Get an array with all posts from specific post type
*
* @param string $post_type retrieves posts by Post Types
*
* @return array with post ID as a key and post title as a value
*/
function get_posts_array( $post_type = 'post' ) {
global $post;
$posts_array = array();
$posts = get_posts( array(
'post_type' => $post_type,
'posts_per_page' => - 1
) );
foreach ( $posts as $post ) : setup_postdata( $post );
$posts_array[ get_the_ID() ] = get_the_title();
endforeach;
wp_reset_postdata();
return $posts_array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment