I need to be able to export the data within WordPress so that each object
is contained in a single row, like a flat table.
Imagine each post type as it’s own database table, containing columns that represent all of the metadata.
Right now, I’m having the hardest time trying extract all of the key / value
pair metadata.
I’ve tried get_posts
and then looping over each post and extracting the metadata with get_post_meta
… but this just produces a bunch of convoluted array data.
What would be the recommended way of doing this?
Problem is that meta values are not unique key/value data.
E.g. with the key ‘OneKey’ you can have a lot of values for the same post.
So, in a flat table, for the column ‘OneKey’ you should have multiple rows where the post id is the same and seems to me is not what you want.
Sure, you can take ony one value treating all meta key as singular, but also a single meta value can contain an array of data… in that case you should save that array as serialized.
Finally note that WP has some hidden meta fields that use for internal scope, e.g. ‘_edit_lock’ or ‘_edit_last’ and others…, probably you’ll want to skip those.
$flatten
will be a one-dimensional array of objects (WP_POST
class instances).