My question is: When I migrated the WordPress database files, found that all the articles under the site are not classified, looked for a long time only to find that the original database is less an association between the classification and the article table, that is, the Wp_term_relationships table. Re-import problem resolution. This article inspires me. Bookmark it here.
It is necessary to implement the functionality of all the articles in this category according to the category directory ID. If the function with WordPress is very simple, but because the author is not very familiar with the topic development, so with pure PHP script directly access to the database implementation of the function.
First, introduce several tables in the WordPress database:
Article Table --wp_posts: article ID(ID), title (Post_title), publish Time (Post_time), alias (post_name) (URL used by article permalink), status (post_ Status), type (Post_type), and so on.
Links Table --wp_links: links and other linked information.
Blog Topic table --wp_options: blog title, description, site URL and other information.
Label Table --wp_terms: Tag number, name, alias, category directory name , alias.
Wp_term_relationships: Object ID (object_id, section corresponding to article ID), object Category ID(term_taxonomy_id, part corresponding Catalog number)
Category Table-wp_term_taxonomy: Word Item Classification label (TERM_TAXONOMY_ID), Word Item number (TERM_ID), type (taxonomy, Table of Contents , label , navigation menu ), number of articles (count)
Wp_users: User table, all background login account
Wp_usermeta: User Information table, user name, email address, description, etc.
Wp_conmments: Comment Form
Wp_commentmeta: Not clear about its function
2. Introduction to the principle of getting a list of articles by catalog number
Obviously in order to realize our function, we need to use three tables of wp_terms, Wp_term_relationships and wp_term_taxonomy.
(1) Assuming that the given classification ID is $cid, then $cid corresponds to the term_taxonomy_id field in the Wp_term_taxonomy table, so we can easily get the number of articles under that category:
$sql Count from wp_term_taxonomy where term_taxonomy_id=$CID and taxonomy = ' category ';
(2) According to TERM_TAXONOMY_ID to obtain the article designator and the title of SQL, need to federate three tables to query, Post_status = ' publish ' refers to the article has been published, post_type= ' post ' refers to the record type is the article, Taxonomy = ' Category ' means the type is a directory. This SQL is long, you can understand the reference relationship of the three tables before you write your own SQL statement or directly with this SQL.
$sql $CID // article type: Log status: Published
3. Display effect
about how to connect directly to a database?
The database name, user name, password, and so on are stored in the Wp-config file in the root directory, so we introduce the page and call the variables in it.
<? PHP include ( "wp-config.php" ) ; // Modify the path as needed $conn = mysql_connect (db_host ,db_user ,Db_password ) ; mysql_select_db (db_name ) ; mysql_query ( "set names Utf-8" ) ; ? >
Source: https://my.oschina.net/shunshun/blog/88692
WordPress Direct Access database lists all articles under the specified category directory