The typical way to exclude pages when using get_pages is like this:
get_pages(array(
"exclude" => "1,2,3"
));
Passing in comma-separated list of page id’s that you wish to exclude is easy for a machine to handle, but makes no sense for people. Also, page ID’s can get changed when migrating the site from your local dev set up onto the production server, and as a client starts editing content and creating/deleting pages. For a million reasons, this approach is flaky at best.
After lots of looking for a way to hack this in using WordPress hooks, I found none. The issue is that the get_pages filter only passes along the resulting array of pages, but there is no way to access the initial query parameters. There is also a way that requires hacking wordpress core files. Anyway, here’s how I ended up doing it:
Add this function to your functions.php file in your theme directory:
function get_ids_from_slugs($slugs){
$slugs = preg_split("/,s?/", $slugs);
$ids = array();
foreach($slugs as $page_slug){
$page = get_page_by_path($page_slug);
array_push($ids, $page->ID);
}
return implode(",", $ids);
}
Then call it when you call get_pages with an exclude option:
get_pages(array(
"exclude" => get_ids_from_slugs("home, hidden-page")
));
It’s not the most elegant solution, but it won’t get stomped if the client updates wordpress, and it doesn’t rely on any obscure facets of wordpress that may change and break its functionality, so it works for me.
No Trackbacks
You can leave a trackback using this URL: http://keithnorm.com/2009/11/22/exclude-pages-by-slug-in-wordpress-get_pages/trackback/
One Comment
I tried to register to your rss feed, but i had a error adding it to google right ascension