Opened 4 years ago
Last modified 2 years ago
#9383 assigned enhancement
Allow get_pages to exclude by slug in addition to id
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Query | Version: | 2.7.1 |
| Severity: | normal | Keywords: | has-patch needs-testing |
| Cc: | brad@…, wojtek.szkutnik@… |
Description
Currently, get_pages() allows template authors to exclude certain pages from the result, by their ID. It's more useful to exclude by slug when the IDs aren't immediately known.
For example, on a WordPress site I recently made, I coded an automatically-generated menu listing the site's pages and their subpages. There are certain classes of pages I want to exclude, and hard-coding the page IDs into my menu code just isn't a good way to do it. Excluding by page slug works beautifully.
Patch: I've included a patch to add this functionality. It is a very short addition to wp-includes/post.php (one new line and two altered lines), has been well used on my (now live) site, and adds a very useful feature.
Syntax: The syntax is identical to the old use, except that where the "exclude" parameter only accepted numbers, it now can take strings. If the value given is not a number, it's treated as a slug. If it is a number, it's treated as an ID like before. For example:
$subPages = get_pages('hierarchical=0&exclude=sidebar&parent='.$page->ID);
will get subpages of $page that don't have the "sidebar" slug. (Numerical values are still treated as IDs, and lists can contain any number of IDs and slugs to exclude.)
Attachments (4)
Change History (14)
- Milestone changed from Unassigned to 2.8
comment:2
williamsba — 4 years ago
- Cc brad@… added
pages-exclude-by-slug.diff tested and working. Tested on nightly build
comment:3
williamsba — 4 years ago
- Keywords tested added
- Keywords needs-patch added; has-patch tested removed
comment:6
janeforshort — 4 years ago
- Milestone changed from 2.8 to Future Release
Punting due to feature freeze. Reconsider with next release.
- Component changed from General to Query
wojtek.szkutnik — 3 years ago
comment:8
wojtek.szkutnik — 3 years ago
- Cc wojtek.szkutnik@… added
- Keywords has-patch needs-testing gsoc added; needs-patch removed
My patch introduces the exclude_slugs argument. It is probably the only way to eliminate the numerical slugs issue.
- Owner changed from anonymous to garyc40
- Status changed from new to assigned
use IN and NOT IN instead of a series of AND id <> %s, handle exclude_slugs and include_slugs
use IN and NOT IN instead of a series of AND id <> %s, handle exclude_slugs and include_slugs
comment:10
garyc40 — 2 years ago
- Keywords gsoc removed

Patch to post.php (get_pages) to allow excluding by slug