Make WordPress Core

Opened 13 years ago

Last modified 5 years ago

#18701 accepted defect (bug)

"hierarchical" argument for get_pages() does nothing

Reported by: viper007bond's profile Viper007Bond Owned by: drewapicture's profile DrewAPicture
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description (last modified by Viper007Bond)

The hierarchical argument for get_pages() is never actually used. It is in an if statement, but one that requires child_of to also be set:

if ( $child_of || $hierarchical )
	$pages = & get_page_children($child_of, $pages);

Props Vynce Montgomery for pointing this out to me.

Change History (8)

#1 @Viper007Bond
13 years ago

  • Description modified (diff)

#2 @nacin
12 years ago

  • Version changed from 3.3 to 3.0

Somewhat related, I've found afew times that it would be helpful for get_pages() to not enforce hierarchical-ness.

#3 @nacin
10 years ago

  • Component changed from General to Post Types

#4 @DrewAPicture
8 years ago

  • Owner set to DrewAPicture
  • Status changed from new to accepted

#5 @DrewAPicture
8 years ago

Took a look at this the other night.

The truly special part about the current state of get_pages() is that because $hierarchical is true and $child_of is 0|false by default, we actually end up with non-hierarchical results by default because $child_of translates to parent = 0 in get_page_children(). To maintain backward-compatibility, we would have to retain that default behavior

Running two rudimentary tests of current behavior, I got surprising results:

$hierarchical = get_pages( array( 'number' => 10 ) );
$hierarchical = wp_list_pluck( $hierarchical, 'post_parent' );
// 5 pages, all with parent 0

$non_hierarchical = get_pages( array( 'hierarchical' => false, 'number' => 10 ) );
$non_hierarchical = wp_list_pluck( $non_hierarchical, 'post_parent' );
// 10 pages, mixed parents

If we wanted to truly implement hierarchical results, there are a couple of things to consider:

  • Like get_terms(), we should probably only set the LIMIT clause if $hierarchical, $child_of and $parent are falsey and $number is not empty. Currently it's always set if $number is not empty (default is empty)
  • It should be documented what exactly "hierarchical" means in terms of returned results

I think the best starting place is to write tests for the current behavior and work outward.

#6 @DrewAPicture
8 years ago

In 34698:

Tests: Add two tests for the current 'hierarchical' argument behavior in get_pages().

See #18701.

#7 @DrewAPicture
8 years ago

In 34699:

Tests: Add two tests for the current behavior of the 'hierarchical' and 'child_of' arguments in get_pages().

See #18701.

#8 @DrewAPicture
8 years ago

In 34746:

Docs: Update the argument descriptions for $child_of and $hierarchical in get_pages() to more closely reflect expected behavior.

See #18701. See #32246.

Note: See TracTickets for help on using tickets.