Open ernilambar opened 6 months ago
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.
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
.
Additional information about this issue.
First, I was able to reproduce this issue with a slightly simpler command set by leaving off the --link
flag during the update call:
Create the menu and items:
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/
Try to move Beta item to position 1:
wp menu item update 5 --position=1
Show that Beta did not move:
wp menu item list sample-menu --format=csv
db_id,type,title,link,position
4,custom,Alpha,https://alpha.com,1
5,custom,Beta,https://beta.com,2
Oddly enough, even though Beta cannot be moved to position 1, Alpha can be moved to position 2
wp menu item update 4 --position=2
wp menu item list sample-menu --format=csv
db_id,type,title,link,position
5,custom,Beta,https://beta.com,1
4,custom,Alpha,https://alpha.com,2
Move Beta to position 2:
wp menu item update 5 --position=2
wp menu item list sample-menu --format=csv
db_id,type,title,link,position
4,custom,Alpha,https://alpha.com,1
5,custom,Beta,https://beta.com,2
Move Beta back to position 1
wp menu item update 5 --position=1
wp menu item list sample-menu --format=csv
db_id,type,title,link,position
5,custom,Beta,https://beta.com,1
4,custom,Alpha,https://alpha.com,2
Move Alpha back to position 1:
wp menu item update 4 --position=1
wp menu item list sample-menu --format=csv
db_id,type,title,link,position
4,custom,Alpha,https://alpha.com,1
5,custom,Beta,https://beta.com,2
wp menu item update 5 --position=1
Show that Beta did not move:
wp menu item list sample-menu --format=csv
db_id,type,title,link,position
4,custom,Alpha,https://alpha.com,1
5,custom,Beta,https://beta.com,2
Edit: I see what's happening. The item update command is always updating the menu_order
column for the posts in the wp_posts
table. When Alpha and Beta share the same order position, they still display in alphabetical order.
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
wp menu item update 1273 --link=https://beta.net --position=1
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