Opened 5 years ago
Closed 5 years ago
#7247 closed enhancement (fixed)
add parent arg to get_pages()
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | 2.7 |
| Component: | Template | Version: | 2.6 |
| Severity: | normal | Keywords: | has-patch needs-testing |
| Cc: |
Description
At present, When using get_pages() with $child_of > 0 & $hierarchical == false, *every* post is called from the database, pages which are not a child of it are then filtered out.
This patch moves the child_of filtering to the SQL query for when heirarchial is not requested, This reduces the number of SQL results.
If hierarchical is true, Then every page is still called up as previous, this is mearly an optimization for the one specific case
Maintains 100% back compat from what i can see.
Eg:
code:
$pages = get_pages( array('child_of' => 14, 'hierarchical' => 0) );
before:
"SELECT * FROM wp_posts WHERE (post_type = 'page' AND post_status = 'publish') ORDER BY post_title ASC"
after:
"SELECT * FROM wp_posts WHERE (post_type = 'page' AND post_status = 'publish') AND post_parent = 14 ORDER BY post_title ASC"
Attachments (2)
Change History (7)
This gets only the direct ancestors, yes? Is the current expectation that child_of without hierarchical will still fetch all generations, just not sorted hierarchically. I think get_terms() uses "parent" to fetch just the immediate ancestors, whereas child_of always returns all of the begats.
Yep, You're right Ryan. (I realised it this morning too :P)
How about adding a parent arg to get_pages() then?
- Component changed from Optimization to Template
- Summary changed from get_pages specific use case optimization to add parent arg to get_pages()
- Type changed from defect to enhancement
Patch attached implements the 'parent' arg. This is useful in themes where a Menu may want to include the Subpages of a page, but wishes to prevent the retrieval of *every* page in order to do so.
Eg: Http://dd32.id.au/ the top menu (Home | Blog | Contact me, etc) are pages which are subpages of 'Frontpage holder', There is no particular reason to retrieve all the plugin pages for that page listing.

Hm, Actually, I'm noticing an extra 2 queries on my site after this, Both after a parent ID, however, I cant see why that would happen, As the args/output stays the same, The cache keys(And cached values) stay the same etc.