Home » Hooks » wp_list_pages_excludes

The WordPress hook wp_list_pages_excludes is a filter hook that allows developers to exclude specific pages from being listed when using the wp_list_pages() function.

The wp_list_pages() function is a useful function in WordPress that generates an unordered list of pages. By default, it includes all published pages in the list. However, there may be cases where you want to exclude certain pages from being displayed.

The wp_list_pages_excludes hook provides a way to modify the list of excluded pages. By adding a custom function to this hook, developers can specify the IDs of the pages they want to exclude. This can be useful in various scenarios, such as excluding pages with a specific template or excluding certain pages from navigation menus.

Here’s an example of how you can use the wp_list_pages_excludes hook to exclude certain pages by ID:

function exclude_pages_from_list( $exclude ) {
    // Specify the IDs of the pages to be excluded
    $excluded_pages = array( 2, 5, 10 );

    // Add the IDs to the existing list of excluded pages
    $exclude .= ',' . implode( ',', $excluded_pages );

    return $exclude;
add_filter( 'wp_list_pages_excludes', 'exclude_pages_from_list' );

In this example, we have created a custom function called exclude_pages_from_list that appends the IDs 2, 5, and 10 to the list of excluded pages. The IDs are added to the existing list using the implode() function to convert the array into a comma-separated string. Finally, the modified list is returned.

By adding this code to your theme’s functions.php file or a custom plugin, the specified pages will be excluded from the output of wp_list_pages(). This gives you greater control over which pages are included in the generated page list.

Remember, the wp_list_pages_excludes hook is specific to wp_list_pages() and cannot be used with other functions or hooks. Make sure to check the official WordPress documentation for more information on how to use this hook effectively.

Learn More on WordPress.org

WordPress snippets using the wp_list_pages_excludes hook

Register an account to save your snippets or go Pro to get more features.