The get_pages() WordPress PHP function retrieves an array of pages or hierarchical post type items.
Usage
$pages = get_pages($args);
Custom Example:
$args = array( 'child_of' => 3, 'sort_order' => 'DESC', 'sort_column' => 'post_date' ); $pages = get_pages($args);
Parameters
$args(array|string) Optional: Array or string of arguments to retrieve pages.child_of(int): Page ID to return child and grandchild pages of. Default 0 (no restriction).sort_order(string): How to sort retrieved pages. Accepts ‘ASC’, ‘DESC’. Default ‘ASC’.sort_column(string): What columns to sort pages by, comma-separated. Default ‘post_title’.hierarchical(bool): Whether to return pages hierarchically. Default true.exclude(int): Array of page IDs to exclude.include(int): Array of page IDs to include.meta_key(string): Only include pages with this meta key.meta_value(string): Only include pages with this meta value. Requires$meta_key.authors(string): A comma-separated list of author IDs.parent(int): Page ID to return direct children of. Default -1 (no restriction).exclude_tree(string|int): Comma-separated string or array of page IDs to exclude.number(int): The number of pages to return. Default 0 (all pages).offset(int): The number of pages to skip before returning. Requires$number. Default 0.post_type(string): The post type to query. Default ‘page’.post_status(string|array): A comma-separated list or array of post statuses to include. Default ‘publish’.
More information
See WordPress Developer Resources: get_pages()
Examples
Displaying pages in a dropdown list
In this example, a dropdown list with all the pages is created. Note how you can grab the link for the page with a simple call to the function get_page_link() passing the ID of the page.
<select name="page-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""> <?php echo esc_attr( __( 'Select page' ) ); ?></option>
<?php
$pages = get_pages();
foreach ( $pages as $page ) {
$option = '<option value="' . get_page_link( $page->ID ) . '">';
$option .= $page->post_title;
$option .= '</option>';
echo $option;
}
?>
</select>
Displaying child pages of the current page in post format
$mypages = get_pages( array(
'child_of' => $post->ID,
'sort_column' => 'post_date',
'sort_order' => 'desc'
));
foreach( $mypages as $page ) {
$content = $page->post_content;
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
?>
<h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
<div class="entry"><?php echo $content; ?></div>
<?php
} ?>
Key-value pairs with page ID and Page Title
This can be very useful for a dropdown list.
$pages = get_pages(array('sort_column' => 'post_date', 'sort_order' => 'desc'));
// build the key-value array
$selectpages = array();
foreach ($pages as $pkey => $page) {
$selectpages[$pkey] = array(
$page->ID => $page->post_title,
);
}
// output
print_r($selectpages);
Displaying pages sorted by the number of comments
$pages_with_comments = get_pages(array(
'sort_column' => 'comment_count',
'sort_order' => 'desc'
));
foreach ($pages_with_comments as $page) {
echo $page->post_title . ' - ' . $page->comment_count . ' comments<br>';
}
Displaying pages from a specific author
$author_id = 2; // replace with the desired author ID
$author_pages = get_pages(array(
'authors' => $author_id
));
foreach ($author_pages as $page) {
echo $page->post_title . ' by author ' . $author_id . '<br>';
}