Skip to content

Fix: wp menu item update --position does not reshuffle other menu items#603

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/fix-position-update-wp-menu-item
Draft

Fix: wp menu item update --position does not reshuffle other menu items#603
Copilot wants to merge 4 commits intomainfrom
copilot/fix-position-update-wp-menu-item

Conversation

Copy link
Contributor

Copilot AI commented Mar 18, 2026

wp menu item update <db-id> --position=N was updating the target item's menu_order but leaving all other items untouched, resulting in duplicate or broken position ordering.

Changes

  • Track old position before update: Saves the current menu_order (normalized: 0 → 1) prior to calling wp_update_nav_menu_item() so the delta can be computed.
  • Range-based reorder after position change: After a successful update where position changed, shifts the affected items in the menu:
    • Moving up (new < old): items in [new_pos, old_pos - 1] shift down by +1
    • Moving down (new > old): items in [old_pos + 1, new_pos] shift up by -1
  • New private method reorder_menu_items_in_range(): Complements the existing reorder_menu_items() (used for add/delete) with a bounded BETWEEN-based SQL update instead of an unbounded >=.
  • New Behat scenario: "Menu order is recalculated on update" — covers moving an item to an earlier position and a later position, verifying the full menu order is consistent after each operation.

Example

wp menu item add-custom sample-menu Alpha https://alpha.com
wp menu item add-custom sample-menu Beta https://beta.com
# positions: Alpha=1, Beta=2

wp menu item update {BETA_ID} --position=1
wp menu item list sample-menu --fields=title,position
# Before fix: Alpha=1, Beta=2  (position unchanged)
# After fix:  Beta=1,  Alpha=2 (correctly reshuffled)
Original prompt

This section details on the original issue you should resolve

<issue_title>Position is not updated in wp menu item update command</issue_title>
<issue_description>## Bug Report

Describe the current, buggy behavior

Position is not updated in wp menu item update command.

Describe how other contributors can replicate this bug

wp menu create "Sample Menu"
wp menu item add-custom sample-menu Alpha https://alpha.com
wp menu item add-custom sample-menu Beta https://beta.com

wp menu item list sample-menu --format=csv

db_id,type,title,link,position
1272,custom,Alpha,https://alpha.com,1
1273,custom,Beta,https://beta.com,2

wp menu item update 1273 --link=https://beta.net --position=1

db_id,type,title,link,position
1272,custom,Alpha,https://alpha.com,1
1273,custom,Beta,https://beta.net,2

Describe what you would expect as the correct outcome

Position also should have been updated.

Let us know what environment you are running this on

OS:     Darwin 22.6.0 Darwin Kernel Version 22.6.0: Tue Nov  7 21:48:06 PST 2023; root:xnu-8796.141.3.702.9~2/RELEASE_X86_64 x86_64
Shell:  /bin/zsh
PHP binary:     /usr/local/Cellar/php@8.2/8.2.15/bin/php
PHP version:    8.2.15
php.ini used:   /usr/local/etc/php/8.2/php.ini
MySQL binary:   /usr/local/bin/mysql
MySQL version:  mysql  Ver 8.3.0 for macos13.6 on x86_64 (Homebrew)
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /Users/nilambarsharma/Sites/staging
WP-CLI packages dir:    /Users/nilambarsharma/.wp-cli/packages/
WP-CLI cache dir:       /Users/nilambarsharma/.wp-cli/cache
WP-CLI global config:   /Users/nilambarsharma/.wp-cli/config.yml
WP-CLI project config:  /Users/nilambarsharma/Sites/staging/wp-cli.local.yml
WP-CLI version: 2.10.0

</issue_description>

Comments on the Issue (you are @copilot in this section)

@danielbachhuber Thanks for the report, @ernilambar !

This definitely seems like a bug. I was able to reproduce with the steps you provided.

With this being said, I'm not sure it ever worked in the first place 🙃

Feel free to submit a pull request, if you'd like. Here is some guidance on our pull request best practices.</comment_new>
<comment_new>@ernilambar
I dug deeper into adding/updating menu item stuffs. Yes, this has not worked till now because when there is new position value, menu_order value of all menu items in that menu should shuffled which we have not done.

We have done only for add:

if ( ( 'add' === $method ) && $menu_item_args['menu-item-position'] ) {
  $this->reorder_menu_items( $menu->term_id, $menu_item_args['menu-item-position'], +1, $result );
}

This reorder_menu_items() only works for an item and makes sense in adding. We cannot reuse this method. We need to introduce separate method for reshuffling menu order for update.</comment_new>
<comment_new>@ernilambar
Related: #493</comment_new>
<comment_new>@ernilambar
@petruchek Yah, not fixed. That approach did not work without breaking other things. Now in block era, this issue does not get enough traction as it is not much used. 😊</comment_new>


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

@github-actions github-actions bot added bug command:menu-item Related to 'menu item' command command:menu-item-update Related to 'menu item update' command labels Mar 18, 2026
@codecov
Copy link

codecov bot commented Mar 18, 2026

Codecov Report

❌ Patch coverage is 91.66667% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Menu_Item_Command.php 91.66% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix position not updated in wp menu item update command Fix: wp menu item update --position does not reshuffle other menu items Mar 18, 2026
Copilot AI requested a review from swissspidy March 18, 2026 08:53
@swissspidy

This comment was marked as resolved.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

…stead of range SQL

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug command:menu-item Related to 'menu item' command command:menu-item-update Related to 'menu item update' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Position is not updated in wp menu item update command

2 participants