开发者

Where does Wordpress store custom menus?

I had to copy a whole wordpress-installation to another server. I simply installed a new Wordpress there and imported all the data from an 开发者_如何学Pythonxml-file previous Wordpress "exported" for me. Anyway, custom menus were not included. Do you know where in the database are they located, so I could grab them from there?


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)
    • _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.

Where does Wordpress store custom menus?

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.

Where does Wordpress store custom menus?

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:

Where does Wordpress store custom menus?

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).

Where does Wordpress store custom menus?

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':

Where does Wordpress store custom menus?

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.

Where does Wordpress store custom menus?

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'.

Where does Wordpress store custom menus?

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.


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.


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 have been searching high and low for the entire structure and I finally cracked the code:

SELECT
p.ID,
m.meta_value,
md.post_author,
wp_users.user_nicename,
p.post_parent,
p.menu_order,
md.post_title
FROM
wp_posts AS p
INNER JOIN wp_postmeta AS m ON m.post_id = p.ID
INNER JOIN wp_posts AS md ON md.ID = m.meta_value AND m.meta_value = md.ID
INNER JOIN wp_users ON md.post_author = wp_users.ID
WHERE
p.menu_order > 0 AND
p.post_type = 'nav_menu_item' AND
m.meta_key = '_menu_item_object_id'
ORDER BY
p.menu_order ASC


To simplify/complexify @random_user_name's two queries, you can condense them into 1 query so that the only input parameter needed is the menu name. Like this:

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_taxonomy      as tax 
  INNER JOIN wp_terms              as t   ON tax.term_id   = t.term_id
  INNER JOIN wp_term_relationships as txr ON t.term_id     = txr.term_taxonomy_id
  INNER JOIN wp_posts              as p   ON txr.object_id = p.ID       AND p.post_status = 'publish' AND p.post_type = 'nav_menu_item'
  LEFT  JOIN wp_postmeta           as m   ON p.ID          = m.post_id  AND m.meta_key    = '_menu_item_url'
  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 tax.taxonomy = 'nav_menu' 
   AND t.name    like '%main nav%'
 ORDER BY p.menu_order


This SQL statement gets the job done a little more thoroughly and legibly than the rest, I think. All you need to do is put your menu name into t.name = 'main nav' on the first join. It outputs every tidbit of menu info except the XFN field.

 SELECT t.name as 'menu name',
        p1.ID as 'post id', p1.post_title as title, p1.post_name as slug, p1.menu_order as 'order',
        m1.meta_value as classes, m2.meta_value as menu_item_parent, m3.meta_value as object, m4.meta_value as object_id,
        m5.meta_value as target, m6.meta_value as type, m7.meta_value as url,
        p2.post_title as 'title-2', p2.post_name as 'slug-2'
   FROM       wp_term_taxonomy      as tax
   INNER JOIN wp_terms              as t   ON t.term_id            = tax.term_id   AND t.name = 'main nav'
   INNER JOIN wp_term_relationships as txr ON txr.term_taxonomy_id = t.term_id
   INNER JOIN wp_posts              as p1  ON p1.ID                = txr.object_id AND p1.post_status = 'publish' AND p1.post_type = 'nav_menu_item'
   LEFT  JOIN wp_postmeta           as m1  ON m1.post_id           = txr.object_id AND m1.meta_key    = '_menu_item_classes'
   LEFT  JOIN wp_postmeta           as m2  ON m2.post_id           = txr.object_id AND m2.meta_key    = '_menu_item_menu_item_parent'
   LEFT  JOIN wp_postmeta           as m3  ON m3.post_id           = txr.object_id AND m3.meta_key    = '_menu_item_object'
   LEFT  JOIN wp_postmeta           as m4  ON m4.post_id           = txr.object_id AND m4.meta_key    = '_menu_item_object_id'
   LEFT  JOIN wp_postmeta           as m5  ON m5.post_id           = txr.object_id AND m5.meta_key    = '_menu_item_target'
   LEFT  JOIN wp_postmeta           as m6  ON m6.post_id           = txr.object_id AND m6.meta_key    = '_menu_item_type'
   LEFT  JOIN wp_postmeta           as m7  ON m7.post_id           = txr.object_id AND m7.meta_key    = '_menu_item_url'
   LEFT  JOIN wp_posts              as p2  ON p2.ID                = m4.meta_value AND p2.post_status = 'publish' AND m6.meta_value = 'post_type'
  WHERE tax.taxonomy = 'nav_menu'
  ORDER BY p1.menu_order


I added additional column on cale_b's query. The column lvl has depth for indentation.

Assume that:

  • max depth is 6, so i defined 6 variables.
  • menu's post_id is over 6. Most of you would've already exceed that number.

Query:

select case when a.meta_value = 0 then least(@lvl:=1, @p1:=a.id) 
            when a.meta_value = @p1 then least(@lvl:=2, @p2:=a.id) 
            when a.meta_value = @p2 then least(@lvl:=3, @p3:=a.id) 
            when a.meta_value = @p3 then least(@lvl:=4, @p4:=a.id) 
            when a.meta_value = @p4 then least(@lvl:=5, @p5:=a.id) 
            when a.meta_value = @p5 then least(@lvl:=6, @p6:=a.id) 
       end lvl
      ,a.id
  from (
        SELECT p.id
              ,p.post_title
              ,p.post_name
              ,pp.meta_value 
              ,p.menu_order
              ,n.post_name as n_name
              ,n.post_title as n_title
              ,m.meta_value meta2
          FROM wordpress_dw.stat_term_relationships as txr 
               INNER JOIN wordpress_dw.stat_posts as p ON txr.object_id = p.ID 
               LEFT JOIN wordpress_dw.stat_postmeta as m ON p.ID = m.post_id and m.meta_key = '_menu_item_url' 
               LEFT JOIN wordpress_dw.stat_postmeta as pl ON p.ID = pl.post_id AND pl.meta_key = '_menu_item_object_id' 
               LEFT JOIN wordpress_dw.stat_postmeta as pp ON p.ID = pp.post_id AND pp.meta_key = '_menu_item_menu_item_parent' 
               LEFT JOIN wordpress_dw.stat_posts as n ON pl.meta_value = n.ID 
              ,(select @lvl:=0, @p1:=0, @p2:=0, @p3:=0, @p4:=0, @p5:=0, @p6:=0) x
         WHERE txr.term_taxonomy_id = 2  -- your menu term id 
           AND p.post_status='publish'
           AND p.post_type = 'nav_menu_item' 
         ORDER BY p.menu_order  
       ) a       

Result:

lvl |id  |
----|----|
1   |508 |
1   |509 |
2   |510 |
3   |511 |
3   |512 |
3   |513 |
3   |514 |
2   |515 |
1   |516 |
2   |517 |
2   |518 |
3   |519 |
3   |520 |
3   |521 |
3   |522 |
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜