Skip to content

Commit f26bdbd

Browse files
Copilotswissspidy
andcommitted
Add wp menu item add-post-type-archive command
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 1603a0b commit f26bdbd

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

features/menu-item.feature

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,24 @@ Feature: Manage WordPress menu items
252252
"""
253253
And the return code should be 1
254254

255+
Scenario: Add a post type archive as a menu item
256+
257+
When I run `wp menu create "Archive Menu"`
258+
Then STDOUT should not be empty
259+
260+
When I run `wp menu item add-post-type-archive archive-menu post --porcelain`
261+
Then STDOUT should be a number
262+
And save STDOUT as {ITEM_ID}
263+
264+
When I run `wp menu item list archive-menu --fields=db_id,type,object`
265+
Then STDOUT should be a table containing rows:
266+
| db_id | type | object |
267+
| {ITEM_ID} | post_type_archive | post |
268+
269+
When I try `wp menu item add-post-type-archive archive-menu invalidposttype`
270+
Then STDERR should be:
271+
"""
272+
Error: Invalid post type.
273+
"""
274+
And the return code should be 1
275+

src/Menu_Item_Command.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,63 @@ public function add_term( $args, $assoc_args ) {
329329
$this->add_or_update_item( 'add', 'taxonomy', $args, $assoc_args );
330330
}
331331

332+
/**
333+
* Adds a post type archive as a menu item.
334+
*
335+
* ## OPTIONS
336+
*
337+
* <menu>
338+
* : The name, slug, or term ID for the menu.
339+
*
340+
* <post-type>
341+
* : Post type slug.
342+
*
343+
* [--title=<title>]
344+
* : Set a custom title for the menu item.
345+
*
346+
* [--link=<link>]
347+
* : Set a custom url for the menu item.
348+
*
349+
* [--description=<description>]
350+
* : Set a custom description for the menu item.
351+
*
352+
* [--attr-title=<attr-title>]
353+
* : Set a custom title attribute for the menu item.
354+
*
355+
* [--target=<target>]
356+
* : Set a custom link target for the menu item.
357+
*
358+
* [--classes=<classes>]
359+
* : Set a custom link classes for the menu item.
360+
*
361+
* [--position=<position>]
362+
* : Specify the position of this menu item.
363+
*
364+
* [--parent-id=<parent-id>]
365+
* : Make this menu item a child of another menu item.
366+
*
367+
* [--porcelain]
368+
* : Output just the new menu item id.
369+
*
370+
* ## EXAMPLES
371+
*
372+
* $ wp menu item add-post-type-archive sidebar-menu post
373+
* Success: Menu item added.
374+
*
375+
* @subcommand add-post-type-archive
376+
*/
377+
public function add_post_type_archive( $args, $assoc_args ) {
378+
379+
$assoc_args['object'] = $args[1];
380+
unset( $args[1] );
381+
382+
if ( ! get_post_type_object( $assoc_args['object'] ) ) {
383+
WP_CLI::error( 'Invalid post type.' );
384+
}
385+
386+
$this->add_or_update_item( 'add', 'post_type_archive', $args, $assoc_args );
387+
}
388+
332389
/**
333390
* Adds a custom menu item.
334391
*

0 commit comments

Comments
 (0)