Wordpress如何将帖子链接到其数据库中的类别? [英] How does Wordpress link posts to categories in its database?
问题描述
目前,我正在使用以下mysql查询在页脚的底部显示网站博客中的最后5条帖子:
At present I am displaying a list of the last 5 posts in a site's blog in its footer using this mysql query:
SELECT post_title, guid, post_date FROM wp_posts WHERE post_type = 'post' AND post_status = 'Publish' ORDER BY post_date DESC LIMIT 5
如何编辑此查询以将搜索限制为特定的类别ID?我认为这就像在posts表中查找类别字段一样简单,但事实并非如此!
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!
推荐答案
可在数据库图.
在您的特定情况下是:
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
要进行查询,您需要使用SQL连接:
For querying you need to use an SQL join:
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
但是请注意,wordpress数据库可能随时更改,因此您应该使用Wordpress提供的机制(例如 query_posts
)从数据库中过滤帖子.
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如何将帖子链接到其数据库中的类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!