I found this code on here that pulls all wordpress pages and displays them in a dropdown list. My question is what needs to be changed to have these pages listed alphabetically?
<form action="">
<select>
<?php // Query for listing all pages in the select box loop
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query( array(
'post_type' => 'page',
'posts_per_page' => -1
));
foreach ($all_wp_pages as $value){
$post = get_page($value);
$title = $post->post_title;
$id = $post->ID;
// For example
// <option value="pageId32">Page title</option>
echo '<option value="pageId' . $id. '">' . $title . '</option>';
}; ?>
</select>
URL of above code: Displaying all pages in dropdown
Thanks in Advance! :) Tracy
You have to add parameters
'orderby' => 'title', 'order' => 'ASC', // or DESC
Please find updated code. .
<?php // Query for listing all pages in the select box loop
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query( array(
'post_type' => 'page',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC', // or DESC
));
foreach ($all_wp_pages as $value){
$post = get_page($value);
$title = $post->post_title;
$id = $post->ID;
// For example
// <option value="pageId32">Page title</option>
echo '<option value="pageId' . $id. '">' . $title . '</option>';
}; ?>
</select>
You have to add sorting in WP_Query
$all_wp_pages = $my_wp_query->query( array(
'post_type' => 'page',
'posts_per_page' => -1,
'orderby' => 'title',
));
add orderby
and order
to query array:
$all_wp_pages = $my_wp_query->query( array(
'post_type' => 'page',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC', // or DESC
));