Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions features/menu-item.feature
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,27 @@ Feature: Manage WordPress menu items
"""
And the return code should be 1

Scenario: Add a post type archive as a menu item

When I run `wp menu create "Archive Menu"`
Then STDOUT should not be empty

When I run `wp menu item add-post-type-archive archive-menu post --porcelain`
Then STDOUT should be a number
And save STDOUT as {ITEM_ID}

When I run `wp menu item list archive-menu --fields=db_id,type,object`
Then STDOUT should be a table containing rows:
| db_id | type | object |
| {ITEM_ID} | post_type_archive | post |

When I run `wp menu item get {ITEM_ID} --field=link`
Then STDOUT should not be empty

When I try `wp menu item add-post-type-archive archive-menu invalidposttype`
Then STDERR should be:
"""
Error: Invalid post type.
"""
And the return code should be 1

63 changes: 63 additions & 0 deletions src/Menu_Item_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,69 @@ public function add_term( $args, $assoc_args ) {
$this->add_or_update_item( 'add', 'taxonomy', $args, $assoc_args );
}

/**
* Adds a post type archive as a menu item.
*
* ## OPTIONS
*
* <menu>
* : The name, slug, or term ID for the menu.
*
* <post-type>
* : Post type slug.
*
* [--title=<title>]
* : Set a custom title for the menu item.
*
* [--link=<link>]
* : Set a custom url for the menu item.
*
* [--description=<description>]
* : Set a custom description for the menu item.
*
* [--attr-title=<attr-title>]
* : Set a custom title attribute for the menu item.
*
* [--target=<target>]
* : Set a custom link target for the menu item.
*
* [--classes=<classes>]
* : Set a custom link classes for the menu item.
*
* [--position=<position>]
* : Specify the position of this menu item.
*
* [--parent-id=<parent-id>]
* : Make this menu item a child of another menu item.
*
* [--porcelain]
* : Output just the new menu item id.
*
* ## EXAMPLES
*
* $ wp menu item add-post-type-archive sidebar-menu post
* Success: Menu item added.
*
* @subcommand add-post-type-archive
*/
public function add_post_type_archive( $args, $assoc_args ) {

$assoc_args['object'] = $args[1];
unset( $args[1] );

$post_type = $assoc_args['object'];

if ( ! get_post_type_object( $post_type ) ) {
WP_CLI::error( 'Invalid post type.' );
}

if ( false === get_post_type_archive_link( $post_type ) ) {
WP_CLI::error( 'Post type does not have an archive.' );
}

$this->add_or_update_item( 'add', 'post_type_archive', $args, $assoc_args );
}

/**
* Adds a custom menu item.
*
Expand Down
Loading