获取带有类别名称,标签和所有相关的帖子

时间:2017-11-05 20:11:37

标签: php mysql wordpress

如何从一行中获取来自数据库的所有帖子,包括类别,标签(如果有),feature_image和其他与帖子相关的帖子。

我有这个附件

SELECT 
wp_posts.ID,
wp_posts.post_title AS title,
wp_posts.post_date AS published_at,
wp_posts.post_content AS content,
wp_posts.post_name AS slug,
files.meta_value AS foto
FROM `wp_posts`
INNER JOIN wp_posts attachments ON wp_posts.ID = attachments.post_parent
INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
WHERE files.meta_key = '_wp_attached_file'

1 个答案:

答案 0 :(得分:0)

在我挖掘更多内容之后,我想出了这个问题:

SELECT  
wp_posts.ID,
wp_terms.name as category,
wp_posts.post_title AS title,
wp_posts.post_date AS published_at,
wp_posts.post_content AS content,
wp_posts.post_name AS slug,
files.meta_value AS foto
FROM
`wp_posts`
LEFT JOIN wp_term_relationships ON(wp_posts.ID =     wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON(wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
LEFT JOIN wp_terms ON(wp_term_taxonomy.term_id = wp_terms.term_id)
INNER JOIN wp_posts attachments ON wp_posts.ID = attachments.post_parent
INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
WHERE files.meta_key = '_wp_attached_file' AND wp_term_taxonomy.taxonomy = 'category' AND wp_posts.post_type = 'post'