Created
November 7, 2014 09:25
-
-
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
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 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