How to Find Details of Menus Using WP-CLI

Knowing certain details about your menu(s) is important for managing them via the command line. For instance, if you want to display a particular menu on your website, you must know the available locations to include in the syntax of the command. However, thanks to the available sub-command list, we can gather the details we need, without having to leave the command line. In this guide, you will learn the steps to find important details about your menu(s) to manage them using WP-CLI.

List Existing Menus

The menus you have created can be listed using the wp menu list command. Follow the steps below to list your menus and obtain the additional information needed to manage them.

  1. SSH into your server as the cPanel user that owns the website you would like to work with.

  2. Use the cd command to switch to the document root directory of your website.

    Example: cd public_html/wp

  3. Run the following command:

    wp menu list

    The output will be similar to the example below:

    exampl3@example.com [~/public_html/wp]# wp menu list +---------+------------------+------------------+-----------+-------+ | term_id | name             | slug             | locations | count | +---------+------------------+------------------+-----------+-------+ | 8       | Primary New Menu | primary-new-menu |           | 0     | | 4       | primary-nav      | primary-nav      | top       | 0     | | 7       | primary-new      | primary-new      |           | 0     | | 5       | social-media     | social-media     | social    | 0     | +---------+------------------+------------------+-----------+-------+

    Notice that the output is formatted nicely displaying the details of the existing menus. For instance, the menu named: primary-nav is assigned to the location: top. This means that the primary-nav menu will be displayed where the top menu is located according to the active theme.

List Available Menu Locations

In order to display a menu on your website you assign the menu a location. Since you must first know the location(s) available to specify in the command, you need to find the location details. The instructions below will guide you through the process of identifying the available menu locations (for the theme that is currently activated), using WP-CLI.

  1. SSH into your server as the cPanel user that owns the website you would like to work with.

  2. Use the cd command to switch to the document root directory of your website.

    Example: cd public_html/wp

  3. Run the following command:

    wp menu location list

  4. The output below, provides an example that lists the available menu locations for your enabled theme.

    exampl3@example.com [~/public_html/wp]# wp menu location list +----------+-------------------+ | location | description       | +----------+-------------------+ | top      | Top Menu          | | social   | Social Links Menu | +----------+-------------------+

    Here, you can see the two (2) available locations, top and social.

List Menu Items

You can review the items included in a particular menu by using the wp menu item list sub-command. Follow the steps below to find the items included in a menu, using WP-CLI.

  1. SSH into your server as the cPanel user that owns the website you would like to work with.

  2. Use the cd command to switch to the document root directory of your website.

    Example: cd public_html/wp

  3. Run the following command:

    wp menu item list primary-nav

    The output below provides an example of the menu items and details listed:

    exampl3@example.com [~/public_html/wp]# wp menu item list primary-nav +-------+-----------+--------------+---------------------------------+----------+ | db_id | type      | title        | link                            | position | +-------+-----------+--------------+---------------------------------+----------+ | 38    | post_type | Home         | http://w...com/wp/              | 1        | | 43    | post_type | About Us     | http://w...com/wp/about-us/     | 2        | | 45    | post_type | Clients      | http://w...com/wp/clients/      | 3        | | 47    | post_type | Gallery      | http://w...com/wp/gallery/      | 4        | | 49    | post_type | Resume       | http://w...com/wp/resume/       | 5        | | 51    | post_type | Info         | http://w...com/wp/info/         | 6        | | 53    | post_type | Testimonials | http://w...com/wp/testimonials/ | 7        | | 55    | post_type | Contact Us   | http://w...com/wp/contact-us/   | 8        | | 23    | taxonomy  | Blog         | http://w...com/wp/./blog/       | 9        | +-------+-----------+--------------+---------------------------------+----------+

Now that you are familiar with how to find the details for your menu(s), you can utilize WP-CLI to manage your menu(s) from the command line. Be sure to check out our guide titled: How to Assign Menus a Location to display your menu(s) on your website.

Leave a Reply