Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Exporting Data from WordPress into a flat table

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.

$posts = get_posts('nopaging=1');
$flatten = array();
$blacklist = array('_edit_lock', '_edit_last', '_wp_old_slug');
if ( ! empty($posts) ) { foreach ( $posts as $post ) {
  $metas = get_post_custom( $post->ID );
  $keys = array_keys($metas);
  if ( ! empty($keys) ) { foreach ( $keys as $_key ) {
     if ( in_array($_key, $blacklist) ) continue;
     // the first value for every meta key
    $post->$_key = maybe_serialize($metas[$_key][0]);
  } } 
  $flatten[$post->ID] = $post;
} }

// for debug
echo '<pre>';
print_r( $flatten );
echo( '</pre>');

$flatten will be a one-dimensional array of objects (WP_POST class instances).

Related Posts:

  1. Sanitizing `wp_editor();` Values for Database, Edit, and Display
  2. Importing New Posts from Live Site into Local Dev Site
  3. What Is meta_id In wp_postmeta?
  4. How much of my site can I recover from public_html?
  5. WordPress Local And Live Site
  6. Display post number by category
  7. Saving Post Data in Another Database
  8. What effect can a large wp_post table have on overall site performance?
  9. Query posts by Author and/or by Tag
  10. How safe is it to delete old posts edits to save database space?
  11. wpColorPicker – problem with implementation to post meta
  12. Change post author without using wp_update_post()
  13. Delete post revisions only for a single post
  14. if in category but only with post meta
  15. Move posts from a non-wordpress site to wordpress
  16. wp_posts table extremely large
  17. How to Assign / Move all Deleted post to a category
  18. wp_query with ajax
  19. Update post meta within save_post action
  20. Updating post meta for checkbox
  21. Custom permalink for each post
  22. How can I tell if a post has been published at least once?
  23. Extract wordpress posts content and category content
  24. Is there a way getting post fields using `transition_post_status`
  25. Automatic blog page with custom design [closed]
  26. Restore woocommerce orders
  27. When moving a WordPress installation to a new server, why do I have to update each post before content shows?
  28. Add multiple meta keys to a post at once
  29. Show number of posts by logged in user
  30. How does WP decide how to display a page
  31. How to select particular month post from table and update the post status using MySQL?
  32. When sending a newsletter -not with wordpress- the server has 100% cpu [closed]
  33. Retrieve data from post meta meta_key having ‘_product_fq_image’ while having author_id and post type is PRODUCT
  34. Insert data from custom created PHP page into wp_postmeta table
  35. Adding a meta box to determine the sidebar [closed]
  36. The loop starting at a certain ID
  37. saving/reading custom field value does not work – no value gets POSTed
  38. Update field in database for users
  39. Simple Custom Metabox Not Saving
  40. Get post category as a separate string and url
  41. Import custom database into wp and keep the post id
  42. why the posts queried from sql is more than those showed on the page?
  43. Extract the last published post
  44. Moved to a new server, backup was a day old and missing 3 posts, anyway to recover them from old host?
  45. Is there a function to get post info for any publish_status by post ID?
  46. Your ideas on my though “delete or move all of the posts in a specific category when 3 days are passed”
  47. How Can I Always Display A Particular Post First Using WP_Query?
  48. Help on conditional statement to accompany wp_insert_post function please?
  49. How can I globally italicize list of texts in the UI database or Posts? [closed]
  50. Is it possible to modify posts table to only read for more recent ones?
  51. Get other posts sharing the same meta values
  52. Featured Images most often doesnt appear
  53. Post meta not working
  54. How to bulk Update URLs to new values?
  55. Select another post in a post meta like a parent page is selected while editing a page
  56. SQL DELETE multiple post_meta on single and multi-sites
  57. Menu Items disappearing/being empty on save or post edit
  58. My website is showing today’s date instead of published date since db migration to a new wordpress
  59. Featured image error after importing blog posts
  60. How to setup default value of post_id
  61. Migrating meta_key and meta_value from old theme to new one
  62. Prevent a post with a specific ID from being created
  63. Private post caught in endless redirect loop
  64. How to mark only one post as Featured post?
  65. Bulk find & replace on WordPress posts/pages (minus image paths)
  66. Show posts from WP Custom Post Type selected from a field in a metabox
  67. How to retrieve all meta data directly from the $post object? Such as $post->related_topics?
  68. What is proper filter or hook action to add data to the $post object?
  69. Reusing old post IDs
  70. Trigger on DB make problem on wordpress?
  71. Blogposting Schema & Structured Data without plugin
  72. Automatically select template based on query string for New Posts
  73. Display Updated Date Instead of Published Date
  74. Comments are in database, don’t show up in WordPress backend or frontend
  75. Is there a way to save different data when USER interacts with the same POST?
  76. Get post by two meta key but order by one of them
  77. How do you update post date (year only) in a separate custom field?
  78. Mix post date with post meta value using WP_Query
  79. Add postmeta to all blog posts
  80. How can I stop ‘in use’ message from intermittently blocking my wp_posts table?
  81. Bulk Post update_post_meta
  82. More Than 50K Categories and WordPress Admin Panel Stop Showing Categories and Posts
  83. Re-order Category Meta-data
  84. how i show manual data in a post
  85. How to hide meta from search result only on pages?
  86. WP_Query sort by meta_value_num or date
  87. WP_Query to get posts in a specific tag or has post_format
  88. Clean up customize_changeset in DB
  89. Grab meta data before post is saved
  90. How to create frontend Post filter using meta query
  91. How do I get a certain set of posts from the database?
  92. How to fetch posts that are stored in the different table than the default table (wp_posts)?
  93. Maximum number of posts per page before affecting performance?
  94. Check if user has avatar
  95. SQL trigger failes with post_content
  96. alt of attached image in post / pages
  97. Creating Ordered Query using Meta_key
  98. Best Way to detect unique posts in wp rest api
  99. Get the correct meta_value with get_post_meta
  100. Surrogate ID for posts, is there an alternative field in the posts table?
Categories posts Tags database, export, meta-query, post-meta, posts
Deleting a user in multisite
API for Post Stats for Self-Hosted WordPress using JetPack [closed]

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress