Make WordPress Core

Opened 13 years ago

Closed 13 years ago

#7247 closed enhancement (fixed)

add parent arg to get_pages()

Reported by: DD32 Owned by:
Milestone: 2.7 Priority: normal
Severity: normal Version: 2.6
Component: Template Keywords: has-patch needs-testing
Focuses: Cc:


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.


$pages = get_pages( array('child_of' => 14, 'hierarchical' => 0) );
"SELECT * FROM wp_posts  WHERE (post_type = 'page' AND post_status = 'publish')     ORDER BY post_title ASC"
"SELECT * FROM wp_posts  WHERE (post_type = 'page' AND post_status = 'publish') AND post_parent = 14  ORDER BY post_title ASC"

Attachments (2)

7247.diff (836 bytes) - added by DD32 13 years ago.
7247.2.diff (1.2 KB) - added by DD32 13 years ago.

Download all attachments as: .zip

Change History (7)

13 years ago

#1 @DD32
13 years ago

  • Keywords needs-testing added

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.

#2 @ryan
13 years ago

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.

#3 @DD32
13 years ago

Yep, You're right Ryan. (I realised it this morning too :P)

How about adding a parent arg to get_pages() then?

13 years ago

#4 @DD32
13 years ago

  • 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.

#5 @ryan
13 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [9145]) Add parent arg to get_pages(). Props DD32. fixes #7247

Note: See TracTickets for help on using tickets.