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: richcon Owned by: garyc40
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)

pages-exclude-by-slug.diff (778 bytes) - added by richcon 4 years ago.
Patch to post.php (get_pages) to allow excluding by slug
9383.diff (2.1 KB) - added by wojtek.szkutnik 3 years ago.
garyc40.9383.diff (3.4 KB) - added by garyc40 2 years ago.
use IN and NOT IN instead of a series of AND id <> %s, handle exclude_slugs and include_slugs
garyc40.9383.2.diff (3.4 KB) - added by garyc40 2 years ago.
use IN and NOT IN instead of a series of AND id <> %s, handle exclude_slugs and include_slugs

Download all attachments as: .zip

Change History (14)

richcon4 years ago

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

  • Milestone changed from Unassigned to 2.8
  • Cc brad@… added

pages-exclude-by-slug.diff tested and working. Tested on nightly build

  • Keywords tested added

comment:4   ryan4 years ago

This will treat numeric slugs as IDs.

IDs must use %d.

  • Keywords needs-patch added; has-patch tested removed
  • Milestone changed from 2.8 to Future Release

Punting due to feature freeze. Reconsider with next release.

  • Component changed from General to Query
  • 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

garyc402 years ago

use IN and NOT IN instead of a series of AND id <> %s, handle exclude_slugs and include_slugs

garyc402 years ago

use IN and NOT IN instead of a series of AND id <> %s, handle exclude_slugs and include_slugs

  • Keywords gsoc removed
Note: See TracTickets for help on using tickets.