Reputation: 1
I have a blog built in laravel , but I want to move all the content to an existing blog built in wordpress, I have tried exporting the database as a CSV file to import on my wordpress database but its not in the same table format , any idea on how to import the contents
Upvotes: 0
Views: 1451
Reputation: 6539
It is less likely to work the direct import since the table structures will be different. So what we can do is to
wpdb
query (https://codex.wordpress.org/Class_Reference/wpdb)wp_insert_post
(https://developer.wordpress.org/reference/functions/wp_insert_post/)You must take WordPress DB backup before doing this.
Sample code will be something like this.
$mydb = new wpdb('username','password','laravel_database','localhost');
$rows = $mydb->get_results("select title, content from laravle_blogs_table");
foreach ($rows as $obj) :
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( $obj->title ),
'post_content' => $obj->content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
// Insert the post into the database
wp_insert_post( $my_post );
endforeach;
You must change the field names as per your database.
This code can be put in any of the active theme files may be header.php
or footer.php
or other template and just load the page from your browser. If there are a huge number of posts put a limit to the select query and insert step by step.
Upvotes: 1