SQL查询以提取具有类别的所有WordPress帖子 [英] SQL query to extract all WordPress posts with categories

查看:77
本文介绍了SQL查询以提取具有类别的所有WordPress帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的WordPress数据库中提取所有帖子以及相关的类别,并且不确定如何编写此查询.我已经不高兴地刺了几下,希望得到帮助吗?

I need to extract all posts from my WordPress DB along with the associated categories and not sure how to write this query. I've taken a couple of stabs at it already with no joy and would appreciate the help?

这是我已经尝试过的内容:

SELECT post_title, wpr.object_id, wp_terms.name
FROM wp_terms
INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr ON wpr.term_taxonomy_id = 
wp_term_taxonomy.term_taxonomy_id
INNER JOIN wp_posts ON ID = wpr.object_id
WHERE taxonomy = 'category' 
AND post_type = 'post' 
ORDER by post_title

这似乎可行,但是它返回1,553,我知道我的数据库中只有1343.

This seems to work but it returns 1,553 where I know I only have 1343 in my DB.

不久前,我们在另一个SQL查询上做了同样的事情,发现它正在引入修订版和其他帖子类型,但认为可以使用post_type ='post'

We did the same thing on another SQL query a little while ago and found that it was pulling in the revisions and other post types but thought that this was resolved using post_type = 'post'

在查看数据库中的类别数量后,我得出总数为216,如果减去1553-1343 = 216,则为总数的6.因此,我认为这1553的总数来自wp_terms表,该表需要被排除在外,仅应显示那些活跃于已发布帖子的帖子?

Upon looking at the number of categories in the DB, I come up with a total number of 216, 6 off the number if you subtract 1553 - 1343 = 216. So I think this total number of 1553 is coming from the wp_terms table which needs to be excluded and only those that are active with published posts should be shown?

另一种可能性是每个帖子可以具有多个类别,因此有更多帖子的原因(1553).那么如何将每个帖子分为多个类别?

The other possibility is that each post can have multiple categories, hence the reason for having more posts (1553). So how could I separate each posts into multiple categories?

非常感谢!

推荐答案

这是对我有用的最终答案.

This is the final answer that worked for me.

SELECT DISTINCT
post_title
, post_content
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Asking Price (US\$)' AND wp_postmeta.post_id = wp_posts.ID) AS "Asking Price (US\$)"
,(SELECT group_concat(wp_terms.name separator ', ') 
    FROM wp_terms
    INNER JOIN wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id
    INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
    WHERE taxonomy= 'category' and wp_posts.ID = wpr.object_id
) AS "Categories"
,(SELECT group_concat(wp_terms.name separator ', ') 
    FROM wp_terms
    INNER JOIN wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id
    INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
    WHERE taxonomy= 'post_tag' and wp_posts.ID = wpr.object_id
) AS "Tags"
FROM wp_posts
WHERE post_type = 'post' 
ORDER BY
post_title
, post_content

这篇关于SQL查询以提取具有类别的所有WordPress帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆