The get_all_page_ids() WordPress PHP function retrieves a list of all page IDs present on your WordPress site.
Usage
$page_ids = get_all_page_ids();
echo '<h3>My Page List:</h3>';
foreach($page_ids as $page) {
echo '<br />'.get_the_title($page);
}
In the above example, get_all_page_ids() function is used to collect all page IDs. Then, within a foreach loop, the get_the_title() function is used to print the title of each page.
Parameters
- This function does not have any parameters.
More information
See WordPress Developer Resources: get_all_page_ids()
This function is included in WordPress since version 2.1.0.
Examples
Display Page IDs and Page Titles
// Get all page IDs
$page_ids = get_all_page_ids();
// Display page ID and title for each page
foreach($page_ids as $page) {
echo 'Page ID: ' . $page . ' Title: ' . get_the_title($page) . '<br />';
}
In this example, the function get_all_page_ids() is used to gather all page IDs, which are then printed alongside their corresponding titles.
Count the Total Number of Pages
// Get all page IDs $page_ids = get_all_page_ids(); // Print the total number of pages echo 'Total Pages: ' . count($page_ids);
This example counts the total number of pages by using the count() function on the array of page IDs returned by get_all_page_ids().
Create an Array of Page Titles
// Get all page IDs
$page_ids = get_all_page_ids();
$page_titles = array();
// Add page titles to the array
foreach($page_ids as $page) {
$page_titles[] = get_the_title($page);
}
Here, an array of page titles is created by fetching the title of each page using get_the_title() inside a foreach loop.
Check if a Page Exists by ID
// Get all page IDs
$page_ids = get_all_page_ids();
$page_to_check = 50;
// Check if a page exists
if(in_array($page_to_check, $page_ids)) {
echo 'Page with ID ' . $page_to_check . ' exists.';
} else {
echo 'Page with ID ' . $page_to_check . ' does not exist.';
}
In this example, the in_array() function is used to check if a page with a specified ID exists.
Display Page IDs in Ascending Order
// Get all page IDs
$page_ids = get_all_page_ids();
// Sort page IDs
sort($page_ids);
// Print sorted page IDs
foreach($page_ids as $page) {
echo 'Page ID: ' . $page . '<br />';
}
In this final example, sort() is used to sort the array of page IDs in ascending order. Then, each sorted page ID is printed.