Make WordPress Core

Opened 13 years ago

Last modified 5 weeks ago

#27326 new defect (bug)

wp_list_pages - exclude parameter changes behaviour depending on depth

Reported by: nosnurg Owned by:
Priority: normal Milestone:
Component: Posts, Post Types Version: 3.8.1
Severity: normal Keywords: good-first-bug has-unit-tests has-patch
Cc: Focuses:

Description

Let's say I have four top level pages with IDs 1,2 and 3 and each has three children, say, (11,12,13), (21,22,23) and (31,32,33).

If I run

<?php wp_list_pages(); ?>

on that I'll get all the pages shown. So it'll be a list like:

1
    11
    12
    13
2
    21
    22
    23
3
    31
    32
    33

If I run

<?php wp_list_pages(exclude=3); ?>

then page 3 will not be shown and the hierarchy will collapse so 31,32,33 appear in the top level of the hierarchy. So what I'll get is:


1
    11
    12
    13
2
    21
    22
    23
31
32
33

Given the existence of a separate exclude_tree argument, that is pretty much what I'd expect to happen, after all it has to show the child pages somehow. If I now introduce a depth argument so it is:

<?php wp_list_pages(depth=3&exclude=3); ?>

I'd expect the same thing to happen as the depth shouldn't make any difference. What actually happens is that the

exclude now behaves similarly to exclude_tree. What I actually get is:

1
  11 12 13
2
  21 22 23

Now that to me doesn't make sense.

Attachments (7)

27326-unit-tests.diff​ (17.8 KB ) - added by MikeHansenMe 13 years ago.
Unit tests for wp_list_pages
27326-unit-tests.2.diff​ (17.4 KB ) - added by MikeHansenMe 13 years ago.
minor updates to tests and cleanup
27326.diff​ (753 bytes ) - added by MikeHansenMe 13 years ago.
27326-unittests.diff​ (1.8 KB ) - added by MikeHansenMe 12 years ago.
see #30284
27326-trunk.png​ (19.7 KB ) - added by rejaulalomkhan 5 weeks ago.
wp_list_pages() before: trunk 8eb72b9
27326-pr14203.png​ (20.7 KB ) - added by rejaulalomkhan 5 weeks ago.
wp_list_pages() after: PR 14203 applied
27326-pr9045.png​ (19.2 KB ) - added by rejaulalomkhan 5 weeks ago.
wp_list_pages() with PR 9045 applied, for comparison

Download all attachments as: .zip

Change History (18)

#1 @nosnurg
13 years ago

Apologies for somewhat untidy formatting, and please note I did mean to write:

1
    11
    12
    13
2
    21
    22
    23

for the last bit.

#2 @nosnurg
13 years ago

The bug, if that is what it is, would seem to remain in 3.9.0.

@MikeHansenMe
13 years ago

Unit tests for wp_list_pages

#3 @MikeHansenMe
13 years ago

I was able to confirm this. I wrote some unit tests for wp_list_pages that test the arguments. The final test in the set is for this ticket.

@MikeHansenMe
13 years ago

minor updates to tests and cleanup

#4 @MikeHansenMe
13 years ago

  • Keywords has-patch needs-testing added

#5 @wonderboymusic
13 years ago

In 28400:

Add unit tests for wp_list_pages().

Props MikeHansenMe.
See #27326.

#6 @DrewAPicture
12 years ago

  • Component General → Posts, Post Types

#7 @MikeHansenMe
11 years ago

27326.diff​ still applies and has unit tests in 27326-unittests.diff​. These tests were added then removed during #30284 because they were failing. The test was for this bug and passes with the patch.

#8 @SirLouen
17 months ago

  • Keywords needs-refresh good-first-bug has-unit-tests needs-patch added; has-patch needs-testing removed

Reproduction Report

Description

✅ This report validates that the issue can be reproduced.

Reproduction Instructions

  1. Add 3 parent pages
  2. Add 3 child pages to each of the 3 parent pages (9 children in total). Will look ​like this
  3. Add the code in supplemental artifacts to a plugin, functions.php or wherever you can execute such code. Remember to edit the ID of 'exclude' to the ID of the 3rd parent page you created
  4. Add the shortcode [custom_pages_list] to a post
  5. Check the post results
  6. 🐞 The whole section of the parent excluded is removed.

https://i.imgur.com/ZGCnQUB.png

Actual Results

  1. ✅ Error condition occurs (reproduced).

Additional notes

It appears that the current bug is still valid in 2025. I've tried to apply this patch, seems not to be working (or I'm not applying as expected). Checking Unit tests, they seem to be correct, covering this bug. Wondering what happened with this report, general unit tests were added, but the report did not progress itself.

cc @MikeHansenMe

Anyway, just in case, given that the solution provided is almost done, including some tests, I believe this could be a suitable candidate for good-first-bug.

Supplemental artifacts

Code for testing

function custom_pages_list_shortcode($atts) {
    $atts = array(
            'exclude' => '11', // ID of the 3rd parent to exclude
            'depth'   => '3',
            'echo'    => 0,
    );
    return wp_list_pages($atts);
}
add_shortcode('custom_pages_list', 'custom_pages_list_shortcode');

This ticket was mentioned in ​PR #9045 on ​WordPress/wordpress-develop by ​@sainathpoojary.


17 months ago
#9

  • Keywords has-patch added; needs-refresh needs-patch removed

Fixes unexpected behavior when using wp_list_pages() with both depth and exclude parameters.

Previously, excluded parent pages would also exclude their children if depth > 0, making exclude behave like exclude_tree. This change updates the walker logic to ensure orphaned children of excluded parents are still displayed when depth is limited, consistent with how exclude works without depth.

A follow-up commit will add unit test coverage for this behavior.

Trac ticket: #27326

This ticket was mentioned in ​PR #14203 on ​WordPress/wordpress-develop by ​@algorithmsunlocks.


5 weeks ago
#10

Builds on #9045 by @SainathPoojary (their test commit is kept as-is) with a different, page-only fix.

Problem

wp_list_pages( 'exclude=3' ) lists the children of page 3 at the top level, but adding any depth (e.g. depth=3&exclude=3) drops them entirely, so exclude behaves like exclude_tree.

Root cause

get_pages() removes the excluded page but keeps its children, whose parent is then not in the list. Walker::walk() only displays such orphans when $max_depth is 0; with a positive depth they are discarded. Leftover elements cannot simply all be shown when depth > 0, because display_element() also leaves depth-trimmed descendants behind — #9045 does this, and with depth=1&exclude=3 it lists every child and grandchild of the other pages at the top level.

Solution

Adds Walker_Page::walk(), which calls parent::walk() and, when the depth is limited, displays pages whose parent is not in the list flat after the hierarchy — the same way Walker::walk() already does for unlimited depth. Descendants of a promoted page are included down to depth levels below it, so a depth covering the whole tree gives the same output as no depth. The base Walker (nav menus, categories, comments) is unchanged.

Behaviour change: with a positive depth, pages whose parent is not listed for other reasons (e.g. a draft or private parent) are now listed, as they already are with the default depth=0. This includes wp_page_menu() fallbacks called with depth=1.

Testing instructions

  1. Create pages P1, P2, P3, each with three children.
  2. On trunk, run wp_list_pages( array( 'depth' => 3, 'exclude' => $p3_id ) ).
  3. Observe: P3's children are missing (without depth they are listed at the top level).
  4. Apply this PR and repeat: P3's children are listed at the top level. With depth => 1: P1, P2 and P3's three children only.

Automated tests

  • Kept from #9045: Tests_Post_wpListPages::test_wp_list_page_combo_exclude_depth
  • Added in tests/phpunit/tests/post/wpListPages.php (all @ticket 27326):
    • test_wp_list_pages_exclude_with_depth_one_promotes_only_orphans
    • test_wp_list_pages_exclude_with_depth_matches_unlimited_depth
    • test_wp_list_pages_exclude_with_depth_limits_promoted_descendants
    • test_wp_list_pages_child_of_with_depth_does_not_repeat_pages (unchanged-behaviour guard)
  • npm run test:php -- --group 27326: 4 of 5 tests fail on trunk, all 5 pass with this PR (the child_of guard passes on both).
  • Multisite: npm run test:php -- -c tests/phpunit/multisite.xml --group 27326 passes.
  • Related groups pass with this PR: post (966 tests, 1 skipped), walker (54), menu (105).
  • PHPCS / PHPCompatibility on changed files: pass.

Not tested / open questions

  • Front end checked in Chrome with Twenty Twenty-Five (shortcode calling wp_list_pages(); screenshots below). Not tested: a classic theme's wp_page_menu() fallback; GitHub CI not yet run.
  • Should promoted pages be nested up to depth instead of listed flat? Flat was chosen to match the existing depth=0 output.
  • The draft/private-parent behaviour change above may warrant a dev note.

Screenshots

Twenty Twenty-Five front end: exclude=P3 with no depth, depth=3 and depth=1. Trunk and this PR screenshots are added below.

---

AI assistance: Yes
Tool(s): Claude Code (Claude Opus 5.5)
Used for: ticket research, reproduction script, assessing #9045, drafting the fix and tests, drafting this description. Final implementation and tests were reviewed and edited by me.

🤖 Generated with ​Claude Code

@rejaulalomkhan
5 weeks ago

wp_list_pages() before: trunk 8eb72b9

@rejaulalomkhan
5 weeks ago

wp_list_pages() after: PR 14203 applied

@rejaulalomkhan
5 weeks ago

wp_list_pages() with PR 9045 applied, for comparison

#11 @rejaulalomkhan
5 weeks ago

Patch Testing Report

Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/14203 (head cf811cc), compared with ​https://github.com/WordPress/wordpress-develop/pull/9045 (head e0d1c8a)

Environment

  • WordPress: 7.2-alpha-63166-src (trunk 8eb72b9)
  • PHP: 8.3.35
  • Server: wordpress-develop local Docker environment (nginx), PHPUnit 9.6.38
  • Database: MySQL 9.7.2
  • Browser: Microsoft Edge (headless, via Playwright)
  • OS: Windows 11
  • Theme: Twenty Twenty-Five 1.5
  • MU Plugins: None activated
  • Plugins: None activated

Steps taken

  1. Created pages P1 > P1-C1 > P1-C1-G1, P2, P3 > P3-C1 > P3-C1-G1, and a draft parent with a published child.
  2. Rendered wp_list_pages() with depth=0, depth=1 and depth=2 (each with exclude=P3), and depth=1 without exclude, on trunk, with 9045 and with 14203 (screenshots below).
  3. Ran --group 27326 (the tests in 14203, which include the original test from 9045) against each version:
    • trunk: 4 of 5 fail (the child_of guard passes).
    • 9045: 2 fail (test_wp_list_pages_exclude_with_depth_one_promotes_only_orphans, test_wp_list_pages_exclude_with_depth_limits_promoted_descendants).
    • 14203: all 5 pass on single site, multisite and with --order-by=reverse; the post, walker and menu groups also pass (1032 tests, 1 skipped).
  4. ✅ Patch is solving the problem: with 14203, P3's children are listed at the top level when a depth is set, and descendants are limited by depth (depth=1: P3-C1 only; depth=2: P3-C1 and P3-C1-G1).

Expected result

  • With exclude=P3 and a depth, P3's children are listed at the top level, as they are without depth, and depth still limits how many levels are shown.

Additional Notes

  • 9045 shows the excluded page's children, but with depth=1 and depth=2 it also lists pages below the depth limit under other parents (e.g. P1-C1-G1 at the top level).
  • With 14203, a page whose parent is a draft (or private) is now listed whenever a depth is set, even without exclude (last column of the screenshots). This matches the existing depth=0 output and is mentioned in the PR description, but it also affects wp_page_menu() fallback menus, which use depth=1.
  • AI assistance: Claude Code (Claude Opus 5.5) was used to run the tests and take the screenshots; I reviewed the results.

Screenshots/Screencast with results

  • Before (trunk):

wp_list_pages() before: trunk 8eb72b9

  • After (14203):

wp_list_pages() after: PR 14203 applied

  • For comparison (9045):

wp_list_pages() with PR 9045 applied, for comparison

Note: See TracTickets for help on using tickets.