The get_pages WordPress PHP filter allows you to modify the retrieved list of pages.
Usage
add_filter('get_pages', 'your_custom_function', 10, 2); function your_custom_function($pages, $parsed_args) { // your custom code here return $pages; }
Parameters
$pages
(WP_Post[]): An array of page objects.$parsed_args
(array): An array ofget_pages()
arguments.
More information
See WordPress Developer Resources: get_pages
Examples
Exclude specific pages from the list
Exclude pages with IDs 5 and 8 from the list of retrieved pages.
add_filter('get_pages', 'exclude_specific_pages', 10, 2); function exclude_specific_pages($pages, $parsed_args) { $excluded_ids = array(5, 8); return array_filter($pages, function($page) use ($excluded_ids) { return !in_array($page->ID, $excluded_ids); }); }
Sort pages by title in ascending order
Sort the retrieved list of pages alphabetically by title.
add_filter('get_pages', 'sort_pages_by_title', 10, 2); function sort_pages_by_title($pages, $parsed_args) { usort($pages, function($a, $b) { return strcmp($a->post_title, $b->post_title); }); return $pages; }
Add a prefix to page titles
Add a custom prefix to the title of each retrieved page.
add_filter('get_pages', 'add_prefix_to_page_titles', 10, 2); function add_prefix_to_page_titles($pages, $parsed_args) { $prefix = 'Custom Prefix: '; foreach ($pages as $page) { $page->post_title = $prefix . $page->post_title; } return $pages; }
Limit the number of retrieved pages
Limit the number of retrieved pages to 5.
add_filter('get_pages', 'limit_retrieved_pages', 10, 2); function limit_retrieved_pages($pages, $parsed_args) { return array_slice($pages, 0, 5); }
Filter pages by custom meta value
Retrieve only pages that have a specific custom meta value.
add_filter('get_pages', 'filter_pages_by_meta_value', 10, 2); function filter_pages_by_meta_value($pages, $parsed_args) { $meta_key = 'your_meta_key'; $meta_value = 'your_meta_value'; return array_filter($pages, function($page) use ($meta_key, $meta_value) { return get_post_meta($page->ID, $meta_key, true) === $meta_value; }); }