Nullvariable Web Consulting: Easily Exclude Pages from Menus – WordPress How To
Don’t want your legal disclaimer page to show up in your WordPress page menu? What about the Privacy Policy? Here’s a simple function you can add to your functions.php (should work with almost any theme) that will let you set a custom tag on a post to exclude it from any WordPress pages list.
Here’s the code that makes it happen:
(replace “themename” with the name of your theme or something unique)
add_filter('wp_list_pages_
function themename_exclude($output = '') {
/* returns posts with a custom exclude tag so they don't display in the menus */
$pages = get_all_page_ids();
foreach ($pages as $pageid) {
$is_excluded = get_post_custom_values("ex
if ($is_excluded[0] == 'yes') {
array_push($output, $pageid);
}
}
return $output;
}

Now you need to add a custom tag to any post that you don’t want to display in any menu lists. Edit or create the page you want to exclude from the menu, scroll down to the “Custom Fields” box (location will vary depending on what plugins you have installed). The first time you use this custom value you’ll need to click “Enter new” then type in “exclude” with a value of “yes” (remove the ” marks). After you’ve used the value once you will just have to click the select box and choose “exclude” and enter a value of “yes”, if you want to allow the page to show up again, simply remove this value.
This code has not be extensively tested and may require modification to work. This code has not been tested with Thesis, WP-Cache or really anything. I just know that it works with the themes that I’ve developed for WordPress 2.6 through 2.8. Use at your own risk.
Nerd Alert!
Keep reading if you’d like to see how it works.
The first line of code:
add_filter('wp_list_pages_excludes','themename_exclude');
tells WP to add our function “themename_exclude” to the filters that it executes when the wp_list_pages() function is called. Filters allow plugins and themes to modify the default output from WP. In this case we’re asking for the filter that runs against the exclude section of wp_list_pages() so that we can add our universal exclusions whenever the function is called.
Next, we define our function: the function grabs the value sent to it by the filter or defaults to a null value.
function themename_exclude($output = '') {
There might be a more efficient way to do this but for now we grab all the pages that WP lists in the database with this line of code:
$pages = get_all_page_ids();
Now we will loop through these pages:
foreach ($pages as $pageid) {
With each page we’ll ask WP if that page has a custom value of “exclude” attached to it. This way we don’t have to pull the whole post from the data base, just this field, if it exists:
$is_excluded = get_post_custom_values("exclude",$pageid);
If the value “exclude” exists and the value is “yes” we add the page id to the array that was provided to us at the start:
if ($is_excluded[0] == 'yes') {
array_push($output, $pageid);
Now we have an array ($output) containing the original values that we’re excluded plus any pages that have the “exclude” custom tag. This lets the customer or us modify what pages are displayed or not on a page by page basis without manually editing every wp_list_pages() tag that shows up inside a theme.
This is a post from the Nullvariable Web Consulting Blog.
Easily Exclude Pages from Menus – WordPress How To

