Skip to content

Instantly share code, notes, and snippets.

@Alfakom-MK
Created October 10, 2022 07:47
Show Gist options
  • Save Alfakom-MK/c2a19aa7e44aab58c490ea9e16397be7 to your computer and use it in GitHub Desktop.
Save Alfakom-MK/c2a19aa7e44aab58c490ea9e16397be7 to your computer and use it in GitHub Desktop.
Wordpress universal post export to CSV. Use it in other CMS import (Wordpress to Drupal example with CSV Importer module). You must copy/rsync the /wp-content/uploads images and documents folder to the new server previously.
SET SESSION group_concat_max_len = 5000; # Larger Group Concat, adjust if having larger galleries or body texts
SELECT "" AS 'nid' # Destination ID
, 'en' AS 'langcode' # Destination language
, wp1.`post_title` AS 'title' # Destination Title
, 'full_html' AS 'body|format' # Format HTML for the destination body
, CONCAT(IFNULL(nullif(LEFT(wp1.`post_content`,150),''),wp1.`post_title`),'...') AS 'body|summary' # First 150 chars from body as destination excerpt or using title if empty body
, CONCAT('<p style="padding-bottom:',GREATEST(ROUND(280-(POSITION('['IN `post_content`) + char_length(`post_content`))/2/56*20,0),0),'px">',IFNULL(nullif(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(wp1.`post_content`,'[','<'),']','>'),'\n','<br>'),'<div','<p'),'/div','/p'),'<table','<div class="table-responsive"><table class="table table-bordered table-hover table-striped"'),'</table>','</table></div>'),'UNHEX('C2A0'), '&nbsp;'),''),wp1.`post_title`),(SELECT CONCAT('<div class="a-ingal">\n<h3>GALLERY</h3>\n<p>',GROUP_CONCAT(REPLACE(REPLACE(CONCAT('<a data-colorbox-gallery="galery" class="colorbox cboxElement" href="',`guid`,'"><img loading="lazy" src="',`guid`,'"></a>'),'http://DOMAIN.COM',''),'https://DOMAIN.COM','') SEPARATOR '\n'),"</p>\n</div>") FROM `wp_posts` WHERE `post_parent` = wp1.`ID` AND `post_type`='attachment')) AS 'body|value' # Body HTML starts with a paragraph, calculates dynamic bottom padding (because of using featured image float left 50% width inline text), replaces div with p, adds br new lines, disables wp gallery and recreates it in plain html with lightbox module, ads BS in table styling, replaces 0xa0 UTF8 with space, returns same domain to local links and images
, 'News' AS 'field_category' # Post category in destination
, CONCAT('wp-content/uploads/',(SELECT DISTINCT wpm2.meta_value FROM wp_posts wp2 INNER JOIN wp_postmeta wpm ON (wp2.ID = wpm.post_id AND wpm.meta_key = '_thumbnail_id') INNER JOIN wp_postmeta wpm2 ON (wpm.meta_value = wpm2.post_id AND wpm2.meta_key = '_wp_attached_file') WHERE wp2.ID = wp1.ID)) AS "field_image" # Thumbnail or Featured image in Destination (Change custom posts and postmeta table names)
, UNIX_TIMESTAMP(post_date) AS 'created' # Creation Date-Time
, UNIX_TIMESTAMP(post_date) AS 'published_at' # Publish Date-Time
, wp1.`ID` AS 'field_wp_post'
FROM wp_posts wp1 # Change custom posts table name
WHERE wp1.`post_type` = 'post' AND wp1.`post_status` = 'publish' # Change source post type if needed and use LIKE instead of = , if multiple post types needed
ORDER BY wp1.`post_date`, wp1.`ID` #Change order if needed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment