Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. <?php
  2. /**
  3. * Get post Lists
  4. *
  5. * @throws \InvalidArgumentException If the post type doesn't exists.
  6. *
  7. * @param string $postType
  8. *
  9. * @return array An array of key value pair where the key is the post name and the value the post title.
  10. */
  11. function getPostList($postType)
  12. {
  13. if (! post_type_exists($postType)) {
  14. throw new \InvalidArgumentException(
  15. sprintf(esc_html__('Post Type %s does not exists.', 'textdomain'), $postType)
  16. );
  17. }
  18.  
  19. $list = array();
  20.  
  21. // Retrieve the posts.
  22. $query = new \WP_Query([
  23. 'post_type' => $postType,
  24. 'posts_per_page' => -1,
  25. 'no_found_rows' => true,
  26. ]);
  27.  
  28. // Create the list.
  29. if ($query->have_posts()) {
  30. foreach ($query->posts as $post) {
  31. $list[$post->post_name] = $post->post_title;
  32. }
  33. }
  34.  
  35. return $list;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement