@@ -120,6 +120,95 @@ function ( $item ) {
120120 $ formatter ->display_items ( $ items );
121121 }
122122
123+ /**
124+ * Gets details about a menu item.
125+ *
126+ * ## OPTIONS
127+ *
128+ * <db-id>
129+ * : Database ID for the menu item.
130+ *
131+ * [--field=<field>]
132+ * : Instead of returning the whole menu item, returns the value of a single field.
133+ *
134+ * [--fields=<fields>]
135+ * : Limit the output to specific fields. Defaults to db_id, type, title, link, position.
136+ *
137+ * [--format=<format>]
138+ * : Render output in a particular format.
139+ * ---
140+ * default: table
141+ * options:
142+ * - table
143+ * - csv
144+ * - json
145+ * - yaml
146+ * ---
147+ *
148+ * ## AVAILABLE FIELDS
149+ *
150+ * These fields are available:
151+ *
152+ * * db_id
153+ * * type
154+ * * title
155+ * * link
156+ * * position
157+ * * menu_item_parent
158+ * * object_id
159+ * * object
160+ * * type_label
161+ * * target
162+ * * attr_title
163+ * * description
164+ * * classes
165+ * * xfn
166+ *
167+ * ## EXAMPLES
168+ *
169+ * # Get details about a menu item with ID 45
170+ * $ wp menu item get 45
171+ * +-------------+----------------------------------+
172+ * | Field | Value |
173+ * +-------------+----------------------------------+
174+ * | db_id | 45 |
175+ * | type | custom |
176+ * | title | WordPress |
177+ * | link | https://wordpress.org |
178+ * | position | 1 |
179+ * +-------------+----------------------------------+
180+ *
181+ * # Get a specific field from a menu item
182+ * $ wp menu item get 45 --field=title
183+ * WordPress
184+ *
185+ * # Get menu item data in JSON format
186+ * $ wp menu item get 45 --format=json
187+ * {"db_id":45,"type":"custom","title":"WordPress","link":"https://wordpress.org","position":1}
188+ */
189+ public function get ( $ args , $ assoc_args ) {
190+
191+ $ db_id = $ args [0 ];
192+
193+ $ menu_item = get_post ( $ db_id );
194+
195+ if ( ! $ menu_item || 'nav_menu_item ' !== $ menu_item ->post_type ) {
196+ WP_CLI ::error ( 'Invalid menu item. ' );
197+ }
198+
199+ /**
200+ * @var object{title: string, url: string, description: string, object: string, object_id: int, menu_item_parent: int, attr_title: string, target: string, classes: string[], xfn: string, type: string, type_label: string, menu_order: int, db_id: int, post_type: string}&\stdClass $menu_item
201+ */
202+ $ menu_item = wp_setup_nav_menu_item ( $ menu_item );
203+
204+ // Correct position inconsistency and protected `url` param in WP-CLI
205+ $ menu_item ->position = ( 0 === $ menu_item ->menu_order ) ? 1 : $ menu_item ->menu_order ;
206+ $ menu_item ->link = $ menu_item ->url ;
207+
208+ $ formatter = $ this ->get_formatter ( $ assoc_args );
209+ $ formatter ->display_item ( $ menu_item );
210+ }
211+
123212 /**
124213 * Adds a post as a menu item.
125214 *
0 commit comments