IT Blog

  • Blog
  • Technology
    • Technology
    • Architecture
    • CMS
    • CRM
    • Web
    • DotNET
    • Python
    • Database
    • BI
    • Program Language
  • Users
    • Login
    • Register
    • Forgot Password?
  • ENEN
    • 中文中文
    • ENEN
Experience IT
In a World of Technology, People Make the Difference.
  1. Home
  2. Technology
  3. CMS
  4. Wordpress
  5. WP-MySQL
  6. Query menu in WordPress

Query menu in WordPress

2018-11-30 816 Views 0 Like 0 Comments

Menu set in WordPress is stored in MySQL database. The rules as followings:

1. menu set

  • identified in wp_term_taxonomy with taxonomy = 'nav_menu'
  • name is stored in wp_terms.name

2. Menu item could be one of the followings:

  • pages, posts,
  • categories, tags,
  • custom links,
  • items that plugin provides.

3. menu item in wp_posts table is marked as post_type = 'nav_menu_item'. And menu item in wp_posts links with category in wp_terms using wp_postmeta:

  • meta_key = '_menu_item_object_id' indicates the term_id in wp_terms.
  • meta_key = '_menu_item_object' identifies the object type.
  • meta_key = '_menu_item_menu_item_parent' indicates the parent menu item.

4. if using category or tag as menu item, wp_terms.name is the menu label,
if using custom links, wp_posts.post_title is the menu label.

5. tag and category are treated as same way for menu. The different between tag and category is the taxonomy value, tag is post_tag, category is category.

6. category description is stored in wp_term_taxonomy.description and wp_posts.content.

Complete menu query like this:

SELECT p.menu_order order,p.ID,parent.meta_value menu_parent, 
   (CASE WHEN t.name is null THEN p.post_title ELSE t.name END) menu,
   p.post_content description
FROM wp_posts p
   LEFT JOIN wp_postmeta m on m.post_id=p.ID AND m.meta_key='_menu_item_object_id'
   LEFT JOIN wp_postmeta parent on parent.post_id=p.ID and parent.meta_key='_menu_item_menu_item_parent'
   LEFT JOIN wp_terms t on t.term_id=m.meta_value
   LEFT JOIN wp_posts pp on pp.ID=parent.meta_value AND parent.meta_value!=0
WHERE p.post_type='nav_menu_item'
order BY p.menu_order

Result

wordpress menu

 2,145 total views,  4 views today

error
fb-share-icon
Tweet
fb-share-icon
IT Team
Author: IT Team

Tags: None
Last updated:2018-11-30

IT Team

This person is lazy and left nothing

Like
< Previous
Next >

Comments

Cancel reply
Chinese (Simplified) Chinese (Simplified) Chinese (Traditional) Chinese (Traditional) English English French French German German Japanese Japanese Korean Korean Russian Russian
Newest Hotspots Random
Newest Hotspots Random
Rich editor not working Making web page scroll down automatically Getting data from Dapper result All Unicode Chars How to keep and display contact form 7 data Common Regular Expressions
Wordpress database model Sending email using gmail SMTP Passing parameters to event handler, setTimeout Get event list with feature image Install SQL Server 2019, Data tools etc. Python - Crawling images from json file
Categories
  • Architecture
  • BI
  • C#
  • CSS
  • Database
  • DotNET
  • Hosting
  • HTML
  • JavaScript
  • PHP
  • Program Language
  • Python
  • Security
  • SEO
  • Technology
  • Web
  • Wordpress

COPYRIGHT © 2021 Hostlike IT Blog. All rights reserved.

This site is supported by Hostlike.com