Where does Wordpress store custom menus?

I've got a site with a Magento install and WordPress install sitting next to each other, and cross-linking.

I recently spent several hours writing a class to drop into the Magento installation so that I could render the WordPress menu as the navigation menu in the Magento site.

Posts here have been helpful, but none of them have completely explained the structure of how WordPress menus are stored. Like many WP things, it is stored in a series of relationships. Here's the structure:

(note that this example presumes a table prefix if of "wp_")

  1. First, it's important to recognize that a menu item can be a post (technically it's a page, but pages are stored in the post table), a category, or it can be a custom link.
  2. Because WP supports multiple menus, you first look in the wp_term_taxonomy table table to find any terms with the taxonomy of 'nav_menu'. Note the term_id from that table.
  3. To find the name and slug of the menu, visit the wp_terms table and find the term with the id noted from step 2 above.
  4. Go to wp_term_relationships table and list all of the records with the term_taxonomy_id that matched the term_id from step 1. The object_id field tells you the wp_post.id record where you can find the menu record.
  5. Finally, go to wp_postmeta to find many elements describing the menu. Of particular interest are:
    • _menu_item_object - the TYPE of menu item (page, custom, or category)
    • _menu_item_object_id - the id of the actual POST (or category, if it's a category) that the menu item references
    • _menu_item_menu_item_parent - the heirarchical parent structure of the MENU (which can be different than the post parent relationships)
  6. _menu_item_url - the slug of the menu item (if it is a custom link menu item)

Sample SQL statements to perform the above described operation:

SELECT t.term_id 
FROM wp_term_taxonomy as tax 
LEFT JOIN wp_terms as t ON tax.term_id = t.term_id 
WHERE taxonomy = 'nav_menu' and name like '%top%'

(looks for a menu item with the name of 'Top', and gets the term id)

SELECT p.ID, p.post_title, p.post_name, p.menu_order, n.post_name as n_name, n.post_title as n_title, m.meta_value, pp.meta_value as menu_parent
FROM wp_term_relationships as txr 
INNER JOIN wp_posts as p ON txr.object_id = p.ID 
LEFT JOIN wp_postmeta as m ON p.ID = m.post_id 
LEFT JOIN wp_postmeta as pl ON p.ID = pl.post_id AND pl.meta_key = '_menu_item_object_id' 
LEFT JOIN wp_postmeta as pp ON p.ID = pp.post_id AND pp.meta_key = '_menu_item_menu_item_parent' 
LEFT JOIN wp_posts as n ON pl.meta_value = n.ID 
WHERE txr.term_taxonomy_id = 3 AND p.post_status='publish' 
    AND p.post_type = 'nav_menu_item' AND m.meta_key = '_menu_item_url' 
ORDER BY p.menu_order

(loads the data for the menu, based on the term_id of 3)

Note that this sql statement will work for pages and custom menus (I don't have any categories, so didn't include that). The data loaded will allow you to build the permalink using the siteurl from the wp_options table, and appending the post_name to the end (technically, it's not getting the parent structure, but WP finds the page/post properly without it)

Update
A commenter asked about assembling the child menu items with the parent menu items. That will need to be done with PHP. Something like below will do that for you:

// run the query from above
$results = $wpdb->get_results('SELECT....');

// declare new variable to store "assembled" menu
$menu = array();

// loop over the items assigning children to parents
foreach( $results AS $row ) {
    // assemble key bits for the menu item
    $item = array(
        // handles custom navigation labels
        'title' => ( $row->post_title ) ? $row->post_title : $row->n_title,
        // handles custom links
        'permalink' => ( $row->meta_value ) ? $row->meta_value : get_permalink( $row->ID ),
        // declares empty placeholder for any child items
        'children' => array()
    );

    // if the menu item has a parent, assign as child of the parent
    if ( $row->menu_parent ) {
        $menu[ $row->menu_parent ][ 'children' ][] = $item;
    } else {
        $menu[ $row->ID ] = $item;
    }
}

var_dump( $menu );

// outputs something like below:
/**
 * array (size=6)
 *  77 => 
 *     array (size=3)
 *       'title' => string 'About Us' (length=8)
 *       'permalink' => string 'http://www.example.com/about' (length=33)
 *       'children' => 
 *        array (size=7)
 *          0 => 
 *            array (size=3)
 *              'title' => string 'Welcome' (length=22)
 *              'permalink' => string 'http://www.example.com/welcome' (length=35)
 *              'children' => 
 *                array (size=0)
 *                  empty
 *          1 => 
 *            array (size=3)
 *              'title' => string 'Mission' (length=20)
 *              'permalink' => string 'http://www.example.com/mission' (length=33)
 *              'children' => 
 *                array (size=0)
 *                  empty
 *  90 => 
 *    array (size=3)
 *      'title' => string 'Contact Us' (length=10)
 *      'permalink' => string 'http://www.example.com/contact' (length=33)
 *      'children' => 
 *        array (size=5)
 *          0 => 
 *            array (size=3)
 *              'title' => string 'Why Us' (length=12)
 *              'permalink' => string 'http://www.example.com/why' (length=35)
 *              'children' => 
 *                array (size=0)
 *                  empty
 *  1258 => 
 *    array (size=3)
 *      'title' => string 'Login' (length=12)
 *      'permalink' => string 'https://customlink.example.com/some/path/login.php' (length=82)
 *      'children' => 
 *        array (size=0)
 *          empty
 */

For people still arriving at this question, I'll put it in simple phpMyAdmin terms.

There are 6 tables involved.

1. wp_term_taxonomy

WordPress keeps each nav menu location as a record in the 'wp_term_taxonomy' table, but the only unique identifier there is a number ID; 1, 2, 3, etc.

enter image description here

You can also see the 'count' figures that show the number of items in each menu location.

This is what's created when you type

register_nav_menu('your-navmenu', 'Your Navmenu');

You will not find the name 'Your Navmenu' anywhere in the database – that is just used for labelling it in the interface.

However, you will find 'your-navmenu' elsewhere.

2. wp_terms

Your actual menus get stored in 'wp_terms'.

WordPress is confusing with this terminology. In the interface, it has 'display locations' and 'menus'. The location is what's created by register_nav_menu(). It's best to consider the 'menus' in the interface as lists of pages.

I've given them the name 'navlist' here.

enter image description here

The menus also get their own IDs, which are often the same as the location IDs (because people often create one menu for one location at the same time), which can get confusing.

This item is created by the 'Menus' page in the admin interface:

enter image description here

3. wp_options

You will see the 'your-navmenu' slug in 'wp_options'. Here, WordPress stores your current settings for the nav menus. It stores it in a serialised array (using PHP's serialize() function).

enter image description here

The original array (obtained using unserialize()) looks like this.

array (
  'custom_css_post_id' => 56,
  'nav_menu_locations' => 
  array (
    'your-navmenu' => 2,
    'another-navmenu' => 3,
  ),
)

This changes according to which menu (by its ID; '2', '3') you set to which location (by its slug; 'your-navmenu', 'another-navmenu').

4. wp_posts

The menu entries themselves are stored in a different table, 'wp_posts'.

You can find these by searching for 'nav_menu_item':

enter image description here

I've cut out most of the columns for brevity and kept the relevant ones.

Here, you can see the 'menu_order' column, which stores their order in the menu they are in.

The menu entries are stored like actual posts, with a post ID and URL (but it will bring a 404 if you visit it, and they have no 'post_content').

All menu items stored as a sub-item will have a 'post_parent' ID. This is the ID of the actual page that their parent links to, not its menu item ID.

5. wp_postmeta

Menu items are linked to their respective pages in the 'wp_postmeta' table.

The menu item ID ('post_id') is stored in relation to the post ID ('meta_value') in the '_menu_item_object_id' rows, while sub-items are linked to their parent items in the '_menu_item_menu_item_parent' rows.

enter image description here

It's easy to get confused here.

'post_id' is the ID of the menu item, not the post. 'meta_value' is the ID of the post, not the menu item, in '_menu_item_object_id' rows, but it's the ID of the parent menu item, not a post, in '_menu_item_menu_item_parent' rows.

6. wp_term_relationships

The links between each menu item and each menu location are stored in 'wp_term_relationships'.

enter image description here

Here, 'object_ID' is the post ID of the menu item (as seen in 'wp_posts'), and 'term_taxonomy_id' is the ID of the menu location (as seen in 'wp_term_taxonomy').

Hope this cleared it up for some people. I know I was very confused at the start.


This setting happens in the wp_posts table. Look in the table for records where the menu_order greater than zero.

select * from wp_posts where menu_order > 0;

It will also give you the name of the option in the wp_options table where the menu option is configured.

select * from wp_options where option_name = "nav_menu_options";

Also be aware that that wordpress import/export tool will not import media (images,video etc) from the media library which are not being used in posts. If you've got stuff that you directly linked to, its not going to be moved either.


I found this just because I was looking for the answer myself. I see your post is quite old, but the answer is in wp_postmeta, run this query:

SELECT *
FROM `wp_postmeta`
WHERE meta_key LIKE '%menu%'
LIMIT 0, 30

You'll find many entries.

Tags:

Wordpress