Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Get post Lists
- *
- * @throws \InvalidArgumentException If the post type doesn't exists.
- *
- * @param string $postType
- *
- * @return array An array of key value pair where the key is the post name and the value the post title.
- */
- function getPostList($postType)
- {
- if (! post_type_exists($postType)) {
- throw new \InvalidArgumentException(
- sprintf(esc_html__('Post Type %s does not exists.', 'textdomain'), $postType)
- );
- }
- $list = array();
- // Retrieve the posts.
- $query = new \WP_Query([
- 'post_type' => $postType,
- 'posts_per_page' => -1,
- 'no_found_rows' => true,
- ]);
- // Create the list.
- if ($query->have_posts()) {
- foreach ($query->posts as $post) {
- $list[$post->post_name] = $post->post_title;
- }
- }
- return $list;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement