SQL Query for compiling Post metadata

I am working on wordpress and I need to generate timely csv files for the client, this has to include basic information about post from wp_posts like title, time & author. In Addition to this, I am using custom fields to store post_count, author name, author_email and few more meta fields.

Structure of wp_postmeta
id, meta_key, meta_value

Read More

Structure of wp_posts
id, title, date

wp_posts & wp_postmeta has 1:N relation.

If I do a simple innerjoin with statement

SELECT * FROM wp_posts INNER JOIN wp_postmeta where wp_postmeta.post_id = wp_posts.id

then I get a row for each meta value, but what I want is each meta value to become a column and only one row per post.

How to write this query?

Please assume this to be a standard wordpress database.

Related posts

Leave a Reply

2 comments

  1. Old question but it popped up in my search. I used GROUP_CONCAT with key/value pairs as in:

    GROUP_CONCAT(pm.meta_key, '=', pm.meta_value ORDER BY pm.meta_key DESC SEPARATOR ',') as meta