2022年6月26日 星期日

Wordpress 的 Table 分析

由於有想要自己用SC設計一個取代(相容) WORDPRESS 的 CMS架站系統,所以,需要研究一下 WordPress 的資料庫結構。

按照功能大致分為五類:
用戶資訊 : wp_users , wp_usermeta
鏈接資訊 : wp_links
文章及評論 : wp_posts , wp_postmeta , wp_comments
分類,鏈接分類,標籤管理 : wp_term , wp_term_relationships , wp_term_taxonomy
全局設置 : wp_options

wp_commentmeta:評論的其他資訊
wp_comments:留言回覆
wp_links:友情鏈接(Blogroll)
wp_options:儲存 WordPress 系統選項和插件、主題配置
wp_postmeta: 存儲文章(包括頁面、上傳文件、修訂)的其他資訊
wp_posts:儲存文章(包括頁面、上傳文件、修訂)
wp_terms:儲存每個目錄、標籤
wp_term_relationships:儲存每個文章、鏈接和對應分類的關係
wp_term_taxonomy:儲存每個目錄、標籤所對應的分類
wp_usermeta:儲存用戶的其他資訊
wp_users:用戶資訊



https://codex.wordpress.org/Database_Description

The following is an outline and description of the database tables created during the standard installation of WordPress. The database structure and the diagram below were last updated in version 4.4.

The only database supported by WordPress is MySQL version 5.0.15 or greater, or any version of MariaDB.

Also see prior versions of Database Descriptions for WordPress 1.5WordPress 2.0WordPress 2.2WordPress 2.3WordPress 2.5WordPress 2.7WordPress 2.8WordPress 2.9, and WordPress 3.3.

Because WordPress interfaces with this database by itself, you as an end user, shouldn't have to worry much about its structure. If you're Writing a Plugin however, you may be interested in learning how WordPress stores its data and relationships. If you have already attempted to use the existing WordPress API to access the data you need but have determined it's not possible without accessing the database directly, WordPress provides the wpdb class to make this task easy.

Database Diagram

The diagram below provides a visual overview of the WordPress database and the relations between the tables created during the WordPress standard installation. The Table Overview below includes additional details on the tables and columns.

(WP 4.4.2 Database diagram)


Please note that within the standard installation of WordPress no integrity between the tables is enforced e.g. between posts and comments. If you are creating a plugin or extension that manipulates the WordPress database, your code should do the housekeeping so that no orphan records remain in the tables e.g. by removing records in other tables with a set of SQL commands when foreign keys are deleted (Don't forget to remind users to backup before such operations).

Table Overview

This section is the overview of all the tables created during the WordPress standard installation. It is followed by specific information of what is in each table.

WordPress 4.4 Tables (12)
Table NameDescriptionRelevant Area(s) of WordPress User Interface
wp_commentmetaEach comment features information called the meta data and it is stored in the wp_commentmeta.
wp_commentsThe comments within WordPress are stored in the wp_comments table.
wp_linksThe wp_links holds information related to the links entered into the Links feature of WordPress. (This feature has been deprecated, but can be re-enabled with the Links Manager plugin.)
wp_optionsThe Options set under the Administration > Settings panel are stored in the wp_options table. See Option Reference for option_name and default values.
wp_postmetaEach post features information called the meta data and it is stored in the wp_postmeta. Some plugins may add their own information to this table.
wp_postsThe core of the WordPress data is the posts. It is stored in the wp_posts table. Also Pages and navigation menu items are stored in this table.
wp_termsThe categories for both posts and links and the tags for posts are found within the wp_terms table.
wp_termmetaEach term features information called the meta data and it is stored in wp_termmeta.
wp_term_relationshipsPosts are associated with categories and tags from the wp_terms table and this association is maintained in the wp_term_relationships table. The association of links to their respective categories are also kept in this table.
wp_term_taxonomyThis table describes the taxonomy (categorylink, or tag) for the entries in the wp_terms table.
wp_usermetaEach user features information called the meta data and it is stored in wp_usermeta.
wp_usersThe list of users is maintained in table wp_users.

Table Details

The following are the specific fields in each of the tables created during the standard WordPress installation.

Table: wp_commentmeta

FieldTypeNullKeyDefaultExtra
meta_idbigint(20) unsigned PRI auto_increment
comment_idbigint(20) unsigned IND0 
meta_keyvarchar(255)YESINDNULL 
meta_valuelongtextYES NULL 

Indexes

KeynameTypeField
PRIMARYPRIMARYmeta_id
comment_idINDEXcomment_id
meta_keyINDEXmeta_key


Table: wp_comments

FieldTypeNullKeyDefaultExtra
comment_IDbigint(20) unsigned PRI auto_increment
comment_post_IDbigint(20) unsigned IND0 
comment_authortinytext    
comment_author_emailvarchar(100) IND  
comment_author_urlvarchar(200)    
comment_author_IPvarchar(100)    
comment_datedatetime  0000-00-00 00:00:00 
comment_date_gmtdatetime IND & IND Pt20000-00-00 00:00:00 
comment_contenttext    
comment_karmaint(11)  0 
comment_approvedvarchar(20) IND Pt11 
comment_agentvarchar(255)    
comment_typevarchar(20)    
comment_parentbigint(20) unsigned IND0 
user_idbigint(20) unsigned  0 

Indexes

KeynameTypeField
PRIMARYPRIMARYcomment_ID
comment_post_IDINDEXcomment_post_ID
comment_approved_date_gmtINDEXcomment_approved
comment_date_gmt
comment_date_gmtINDEXcomment_date_gmt
comment_parentINDEXcomment_parent
comment_author_emailINDEXcomment_author_email


Table: wp_links

FieldTypeNullKeyDefaultExtra
link_idbigint(20) unsigned PRI auto_increment
link_urlvarchar(255)    
link_namevarchar(255)    
link_imagevarchar(255)    
link_targetvarchar(25)    
link_descriptionvarchar(255)    
link_visiblevarchar(20) INDY 
link_ownerbigint(20) unsigned  1 
link_ratingint(11)  0 
link_updateddatetime  0000-00-00 00:00:00 
link_relvarchar(255)    
link_notesmediumtext    
link_rssvarchar(255)    

Indexes

KeynameTypeField
PRIMARYPRIMARYlink_id
link_visibleINDEXlink_visible


Table: wp_options

FieldTypeNullKeyDefaultExtra
option_idbigint(20) unsigned PRI auto_increment
option_namevarchar(64) UNI  
option_valuelongtext   
autoloadvarchar(20) INDyes 

Indexes

KeynameTypeField
PRIMARYPRIMARYoption_id
option_nameUNIQUEoption_name
autoloadINDEXautoload

Table: wp_postmeta

FieldTypeNullKeyDefaultExtra
meta_idbigint(20) unsigned PRI auto_increment
post_idbigint(20) unsigned IND0 
meta_keyvarchar(255)YESINDNULL 
meta_valuelongtextYES NULL 

Indexes

KeynameTypeField
PRIMARYPRIMARYmeta_id
post_idINDEXpost_id
meta_keyINDEXmeta_key


Table: wp_posts

FieldTypeNullKeyDefaultExtra
IDbigint(20) unsigned PRI & IND Pt4 auto_increment
post_authorbigint(20) unsigned IND0 
post_datedatetime IND Pt30000-00-00 00:00:00 
post_date_gmtdatetime  0000-00-00 00:00:00 
post_contentlongtext    
post_titletext    
post_excerpttext    
post_statusvarchar(20) IND PT2publish 
comment_statusvarchar(20)  open 
ping_statusvarchar(20)  open 
post_passwordvarchar(20)    
post_namevarchar(200) IND  
to_pingtext    
pingedtext    
post_modifieddatetime  0000-00-00 00:00:00 
post_modified_gmtdatetime  0000-00-00 00:00:00 
post_content_filteredlongtext   
post_parentbigint(20) unsigned IND0 
guidvarchar(255)    
menu_orderint(11)  0 
post_typevarchar(20) IND Pt1post 
post_mime_typevarchar(100)    
comment_countbigint(20)  0 

Indexes

KeynameTypeField
PRIMARYPRIMARYID
post_nameINDEXpost_name
type_status_dateINDEXpost_type
post_status
post_date
ID
post_parentINDEXpost_parent
post_authorINDEXpost_author


Table: wp_terms

FieldTypeNullKeyDefaultExtra
term_idbigint(20) unsigned PRI auto_increment
namevarchar(200) IND  
slugvarchar(200) MUL  
term_groupbigint(10)  0 

Indexes

KeynameTypeField
PRIMARYPRIMARYterm_id
slugUNIQUEslug
nameINDEXname

Table: wp_termmeta

FieldTypeNullKeyDefaultExtra
meta_idbigint(20) unsigned PRI auto_increment
term_idbigint(20) unsigned IND0 
meta_keyvarchar(255)YESINDNULL 
meta_valuelongtextYES NULL 

Indexes

KeynameTypeField
PRIMARYPRIMARYmeta_id
term_idINDEXterm_id
meta_keyINDEXmeta_key


Table: wp_term_relationships

FieldTypeNullKeyDefaultExtra
object_idbigint(20) unsigned PRI Pt10 
term_taxonomy_idbigint(20) unsigned PRI Pt2 & IND0 
term_orderint(11)  0 

Indexes

KeynameTypeField
PRIMARYPRIMARYobject_id
term_taxonomy_id
term_taxonomy_idINDEXterm_taxonomy_id


Table: wp_term_taxonomy

FieldTypeNullKeyDefaultExtra
term_taxonomy_idbigint(20) unsigned PRI auto_increment
term_idbigint(20) unsigned UNI Pt10 
taxonomyvarchar(32) UNI Pt2 & IND  
descriptionlongtext    
parentbigint(20) unsigned  0 
countbigint(20)  0 

Indexes

KeynameTypeField
PRIMARYPRIMARYterm_taxonomy_id
term_id_taxonomyUNIQUEterm_id
taxonomy
taxonomyINDEXtaxonomy


Table: wp_usermeta

FieldTypeNullKeyDefaultExtra
umeta_idbigint(20) unsigned PRI auto_increment
user_idbigint(20) unsigned IND0 
meta_keyvarchar(255)YesINDNULL 
meta_valuelongtextYes NULL 

Indexes

KeynameTypeField
PRIMARYPRIMARYumeta_id
user_idINDEXuser_id
meta_keyINDEXmeta_key

Table: wp_users

FieldTypeNullKeyDefaultExtra
IDbigint(20) unsigned PRI auto_increment
user_loginvarchar(60) IND  
user_passvarchar(64)    
user_nicenamevarchar(50) IND  
user_emailvarchar(100)    
user_urlvarchar(100)    
user_registereddatetime  0000-00-00 00:00:00 
user_activation_keyvarchar(60)   
user_statusint(11)  0 
display_namevarchar(250)    


NOTE: Enabling Multisite feature of WordPress adds two fields in wp_users table: spam and deleted. Refer the Multisite version.

Indexes

KeynameTypeField
PRIMARYPRIMARYID
user_login_keyINDEXuser_login
user_nicenameINDEXuser_nicename

Multisite Table Overview

This section is the overview of the tables created for use with the Multisite feature of WordPress. These tables are created via the processes under Administration > Tools > Network.

These tables are considered the multisite global tables.

WordPress 3.0 Multisite Tables
Table NameDescriptionRelevant Area(s) of WordPress User Interface
wp_blogsEach site created is stored in the table, wp_blogs.
wp_blog_versionsThe current database version status of each site is maintained in the wp_blogs_versions table and is updated as each site is upgraded.
wp_registration_logThe wp_registration_log records the admin user created when each new site is created.
wp_signupsThis table holds the user that have registered for a site via the login registration process. User registration is enable in Administration > Super Admin > Options.
wp_siteThe wp_site table contains the main site address.
wp_sitemetaEach site features information called the site data and it is stored in wp_sitemeta. Various option information, including the site admin is kept in this table.
wp_usersThe list of all users is maintained in table wp_users. Multisite add two fields not in the stand-alone version.
wp_usermetaThis table is not re-created for multisite, but meta data of users for each site are stored in wp_usermeta.
Site Specific TablesThe data of the main site are stored in existing unnumbered tables. The data of additional sites are stored in new numbered tables.



Multisite Table Details

The following describe the tables and fields created during the network installation. Note that a global set of tables is created upon creation of the network, and site-specific tables are established as each site is created.

Table: wp_blogs

FieldTypeNullKeyDefaultExtra
blog_idbigint(20) PRI auto_increment
site_idbigint(20)  0 
domainvarchar(200) IND Pt1  
pathvarchar(100) IND Pt2  
registereddatetime  0000-00-00 00:00:00 
last_updateddatetime  0000-00-00 00:00:00 
publictinyint(2)  1 
archivedtinyint(2)  0 
maturetinyint(2)  0 
spamtinyint(2)  0 
deletedtinyint(2)  0 
lang_idint(11) IND0 


NOTE: The blog_id identifies the site and the site_id identifies the network. It is the trace of the past when the 'site' was called as 'blog' in WordPress 2.x. If you add your site in the network, the new record will be inserted to this table wp_blogs with incremented blog_id and the same site_id.


Indexes

KeynameTypeField
PRIMARYPRIMARYblog_id
domainINDEXdomain
path
lang_idINDEXlang_id


Table: wp_blog_versions

FieldTypeNullKeyDefaultExtra
blog_idbigint(20) PRI0 
db_versionvarchar(20) IND  
last_updateddatetime  0000-00-00 00:00:00 

Indexes

KeynameTypeField
PRIMARYPRIMARYblog_id
db_versionINDEXdb_version


Table: wp_registration_log

FieldTypeNullKeyDefaultExtra
IDbigint(20) PRI auto_increment
emailvarchar(255)    
IPvarchar(30) IND  
blog_idbigint(20)  0 
date_registereddatetime  0000-00-00 00:00:00 

Indexes

KeynameTypeField
PRIMARYPRIMARYID
IPINDEXIP

Table: wp_signups

FieldTypeNullKeyDefaultExtra
signup_idbigint(20) PRI auto_increment
domainvarchar(200) IND Pt1  
pathvarchar(100) IND Pt2  
titlelongtext    
user_loginvarchar(60) IND Pt1  
user_emailvarchar(100) IND & IND Pt2  
registereddatetime  0000-00-00 00:00:00 
activateddatetime  0000-00-00 00:00:00 
activetinyint(1)    
activation_keyvarchar(50) IND  
metalongtextYes NULL 

Indexes

KeynameTypeField
signup_idPRIMARYsignup_id
activation_keyINDEXactivation_key
user_emailINDEXuser_email
user_login_emailINDEXuser_login
user_email
domain_pathINDEXdomain
path

Table: wp_site

FieldTypeNullKeyDefaultExtra
idbigint(20) PRI auto_increment
domainvarchar(200) IND Pt1  
pathvarchar(100) IND Pt2  

Indexes

KeynameTypeField
PRIMARYPRIMARYid
domainINDEXdomain
path


Table: wp_sitemeta

FieldTypeNullKeyDefaultExtra
meta_idbigint(20) PRI auto_increment
site_idbigint(20)  0 
meta_keyvarchar(255)YesINDNULL 
meta_valuelongtextYesINDNULL 

Indexes

KeynameTypeField
PRIMARYPRIMARYmeta_id
meta_keyINDEXmeta_key
site_idINDEXsite_id


Table: wp_users

FieldTypeNullKeyDefaultExtra
IDbigint(20) unsigned PRI auto_increment
user_loginvarchar(60) IND  
user_passvarchar(64)    
user_nicenamevarchar(50) IND  
user_emailvarchar(100)    
user_urlvarchar(100)    
user_registereddatetime  0000-00-00 00:00:00 
user_activation_keyvarchar(60)   
user_statusint(11)  0 
display_namevarchar(250)    
spamtinyint(2)  0 
deletedtinyint(2)  0 

Indexes

KeynameTypeField
PRIMARYPRIMARYID
user_login_keyINDEXuser_login
user_nicenameINDEXuser_nicename

Site Specific Tables

When a new additional site is created, the site-specific tables, similar to the stand-alone tables above, are created. Each set of tables for a site are created with the site ID (blog_id) as part of the table name. These are the tables that would be created for site ID 2 and table_prefix wp_:


The data of the main site are stored in unnumbered tables.


















wp_posts

  • ID: bigint(20)
  • post_author: bigint(20)
  • post_date: datetime
  • post_content: longtext
  • post_title: text
  • post_excerpt: text
  • post_status: varchar(20)
  • comment_status: varchar(20)
  • post_password: varchar(255)
  • post_name: varchar(200)
  • post_modified: datetime
  • post_parent: bigint(20)
  • guid: varchar(255)
  • menu_order: int(11)
  • post_type: varchar(20)
  • post_mime_type: varchar(100)
  • comment_count: bigint(20)


post_status:

  1. Publish: Viewable by any site visitor.
  2. Future: Scheduled to be published in a future date.
  3. Draft: This is an incomplete post that's not ready for publication.
  4. Pending: Awaiting a user with higher permissions to publish.
  5. Private: Viewable only to WordPress users at the Administrator level.
  6. Trash: These posts are waiting for deletion.
  7. Auto-Draft: Revisions that WordPress saves automatically while you are editing.
  8. Inherit: This allows a child post (such as Attachments and Revisions) to automatically adopt the same status as its parent post.
WordPress Revisions system:修訂版本系統
Auto-Draft: 編輯時,配合修訂版本系統的自動儲存

wp-config.php:
define( 'WP_POST_REVISIONS', 3 );

WP_POST_REVISIONS: 

  • true (default), -1: store every revision 
  • false, 0: do not store any revisions (except the one autosave per post) 
  • (int) > 0: store that many revisions (+1 autosave) per post. Old revisions are automatically deleted.


Post Types:

There are several default Post Types readily available to users or internally used by the WordPress installation. The most common are:

  • Post (Post Type: ‘post’)
  • Page (Post Type: ‘page’)
  • Attachment (Post Type: ‘attachment’)
  • Revision (Post Type: ‘revision’)
  • Navigation menu (Post Type: ‘nav_menu_item’)
  • Block templates (Post Type: ‘wp_template’)
  • Template parts (Post Type: ‘wp_template_part’)
  • forum: 討論區
    • topic: 討論區 -> 主題
    • reply: 討論區 -> 主題-> 回覆
  • nav_menu_item: 選單

Post:

Posts are used in blogs. They are:

  • displayed in reverse sequential order by time, with the newest post first
  • have a date and time stamp
  • may have the default taxonomies of categories and tags applied
  • are used for creating feeds

The template files that display the Post post type are:

  • single and single-post
  • category and all its iterations
  • tag and all its iterations
  • taxonomy and all its iterations
  • archive and all its iterations
  • author and all its iterations
  • date and all its iterations
  • search
  • home
  • index

Page:

Pages are a static Post Type, outside of the normal blog stream/feed. Their features are:

  • non-time dependent and without a time stamp
  • are not organized using the categories and/or tags taxonomies
  • can be organized in a hierarchical structure — i.e. pages can be parents/children of other pages

The template files that display the Page post type are:

  • page and all its iterations
  • front-page
  • search
  • index

Attachments:

Attachments are commonly used to display images or media in content, and may also be used to link to relevant files. Their features are:

  • contain information (such as name or description) about files uploaded through the media upload system
  • for images, this includes metadata information stored in the wp_postmeta table (including size, thumbnails, location, etc)

The template files that display the Attachment post type are:

  • MIME_type
  • attachment
  • single-attachment
  • single
  • index






沒有留言:

張貼留言

如何判斷現在FORM是在 insert mode? 還是 update mode?

只要用  if (empty({primary_key})) 就可以知道是否為新增模式了。 如果 {promary_key} 是空白的,那麼就是在新增模式;反之,就是更新模式。 以上。