MySQL Wordpress 如何将帖子链接到其数据库中的类别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2848505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:04:02  来源:igfitidea点击:

How does Wordpress link posts to categories in its database?

mysqlwordpress

提问by bcmcfc

At present I am displaying a list of the last 5 posts in a site's blog in its footer using this mysql query:

目前,我正在使用此 mysql 查询在其页脚中显示站点博客中最后 5 篇文章的列表:

SELECT post_title, guid, post_date FROM wp_posts WHERE post_type = 'post' AND post_status = 'Publish' ORDER BY post_date DESC LIMIT 5

How can I edit this query to restrict the search to a particular category id? I thought it would be as simple as looking for a category field in the posts table but it isn't!

如何编辑此查询以将搜索限制为特定类别 ID?我认为这就像在帖子表中查找类别字段一样简单,但事实并非如此!

回答by nikc.org

The relations of the Wordpress database is available in the database diagram.

Wordpress 数据库的关系在数据库图表中可用。

In your particular case it is:

在您的特定情况下,它是:

wp_posts.ID
->wp_term_relationships.object_id
->wp_term_relationships.term_taxonomy_id
->wp_term_taxonomy.term_taxonomy_id
->wp_term_taxonomy.term_id
->wp_terms.term_id

wp_posts.ID
-> wp_term_relationships.object_id
-> wp_term_relationships.term_taxonomy_id
-> wp_term_taxonomy.term_taxonomy_id
-> wp_term_taxonomy.term_id
->wp_terms.term_id

For querying you need to use an SQL join:

对于查询,您需要使用 SQL 连接:

SELECT p.ID, t.term_id
FROM wp_posts p
LEFT JOIN wp_term_relationships rel ON rel.object_id = p.ID
LEFT JOIN wp_term_taxonomy tax ON tax.term_taxonomy_id = rel.term_taxonomy_id
LEFT JOIN wp_terms t ON t.term_id = tax.term_id

But it should be noted that the wordpress database might change at any time, and you should use the Wordpress provided mechanisms (such as query_posts) to filter posts from the database.

但需要注意的是,wordpress 数据库可能随时发生变化,您应该使用 Wordpress 提供的机制(例如query_posts)从数据库中过滤帖子。