Creating WordPress Page Array & Dropdown Selection

Yesterday, while working on a new theme, I realized that I needed to provide a drop-down option in my theme panel that would allow users to choose a page from a list of all the pages they had published. To achieve this, I had to create an array that would gather all the user’s pages so that I could then display them in my options panel. In this article, I will guide you on how to store an array of WordPress pages for use in your theme or plugin, as well as how to create a dropdown of pages.

Creating an Array of Pages

To create an array of pages, you will need to use the code snippet below. This code gathers a list of your pages and stores them in the $pages_array variable so that you can loop through them later. Although the get_pages function already returns an array, the purpose of this snippet is to create a more simple associative array where the keys are the page ID numbers and the value is the page name.


$pages_array = array( ‘Choose A Page’ );

$get_pages = get_pages( ‘hide_empty=0’ );

foreach ( $get_pages as $page ) {

$pages_array[$page->ID] = esc_attr( $page->post_title );



Now that you have an associative array of pages, you can easily loop through it, store it in a global variable, or use it however you want.

Creating a Select Field Dropdown of Pages

To create a select dropdown where a user can select a page from a form, you can loop through an array of pages (as mentioned in the previous section). However, WordPress has a built-in function that was added in WP 2.1 so that you can automatically create a select dropdown using a simple function named “wp_dropdown_pages” that accepts various parameters.

Below is an example of the function in action:


wp_dropdown_pages( array(

‘child_of’ => 0,

‘sort_order’ => ‘ASC’,

‘sort_column’ => ‘post_title’,

‘hierarchical’ => 1,

‘post_type’ => ‘page’

) );


The wp_dropdown_pages function accepts an array of parameters that you can use to customize the dropdown. For example, you can specify the parent page ID, the sort order, the sort column, and the post type.


In conclusion, creating an array of WordPress pages and a dropdown of pages is a simple process that can be achieved using built-in WordPress functions. By following the steps outlined in this article, you can easily create a dropdown of pages for your users to choose from. If you want to learn more about wp_dropdown_pages, you can refer to the WordPress Codex.

Stay in Touch


Related Articles