SQL query to extract all WordPress posts with categories

前端 未结 1 1689
长情又很酷
长情又很酷 2020-12-25 09:24

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

相关标签:
1条回答
  • 2020-12-25 10:13

    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
    
    0 讨论(0)
提交回复
热议问题