Importing data from non-wordpress mysql database


Hits: 3434  

You have to create an import php file to get the Questions/Answers from a non-wordpress database and bring them into posts in wordpress.

Question Table

<?php

/// non-wordpress database connection string here

require('./wp-load.php');

$results = mysql_query("SELECT * FROM questions");

while ($row = mysql_fetch_assoc($results)) 
{

    $post_information = array(
	'post_title' => wp_strip_all_tags( $row['question'] ),
	'post_content' => $row['answer'],
	'post_category' =>  array(6,7), // Cat Ids
	'post_date' => date('Y-m-d H:i:s', strtotime( $row['timestamp'] )),
	'post_type' => "post",
	'post_excerpt' => "",
	'post_author' => "admin",
	'post_status' => "publish"
	);
	
	$post_id = wp_insert_post( $post_information );		
	
	$filename = "http://www.domain.com/images/post-image.png";   ////  Post Thumbnail Image
	$wp_filetype = wp_check_filetype(basename($filename), null );
	
	$attachment = array(
	  'post_mime_type' => $wp_filetype['type'],
	  'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
	  'post_content' => '',
	  'post_status' => 'inherit'
	);
	
	$attach_id = wp_insert_attachment( $attachment, $filename, $new_post );
	
	//update_post_meta($post_id,'_thumbnail_id',$attach_id);	
	
	add_post_meta($post_id, '_thumbnail_id', $attach_id, true);

}

?>

Published by

Sandeep Verma

I’m an Entrepreneur. I’m proud to work as Blogger, LAMP Programmer, Linux Admin, Web Consultant, Cloud Manager, Apps Developer, Searcher. Concentrate > Observe > Imagine > Launch

26 thoughts on “Importing data from non-wordpress mysql database”

  1. I like the helpful info you provide in your articles.
    I will bookmark your blog and check again here regularly. I am quite sure
    I will learn many new stuff right here! Best of luck for the next!

  2. Incredible! This blog looks exactly like my old one! It’s on a totally different subject but it has pretty much the same layout and design. Great choice
    of colors!

  3. fantastic submit, very informative. I wonder why the opposite experts of this sector
    do not notice this. You should continue your writing.

    I am sure, you’ve a great readers’ base already!

  4. I’ve been surfing online more than 4 hours today, yet I never found any interesting article like yours.
    It is pretty worth enough for me. In my opinion, if all website
    owners and bloggers made good content as you did, the web will be a lot more useful than ever before.

  5. Hey There. I found your weblog the usage of msn. This
    is an extremely well written article. I will be sure
    to bookmark it and come back to learn extra of your helpful information. Thank you for the post.
    I will definitely comeback.

  6. This is really interesting, You’re a very skilled blogger.
    I’ve joined your rss feed and look forward to seeking more of your magnificent post.
    Also, I have shared your site in my social networks!

  7. hey there and thank you for your information – I have
    definitely picked up anything new from right here.
    I did however expertise a few technical issues using this web site,
    as I experienced to reload the web site lots of times previous to I could get
    it to load properly. I had been wondering if your hosting is
    OK? Not that I’m complaining, but sluggish loading instances times will very
    frequently affect your placement in google and could damage your high quality score
    if ads and marketing with Adwords. Well I’m adding this RSS to my e-mail and could look out
    for a lot more of your respective intriguing content.
    Make sure you update this again soon.

  8. What’s Taking place i’m new to this, I stumbled upon this I have
    found It absolutely helpful and it has helped me out loads.
    I am hoping to contribute & help different users like
    its aided me. Good job.

  9. I do not even understand how I ended up right here, but I assumed this publish used
    to be great. I do not recognise who you’re but definitely you’re going to
    a famous blogger for those who are not already. Cheers!

  10. Hmm it appears like your site ate my first comment (it was extremely
    long) so I guess I’ll just sum it up what I submitted and say,
    I’m thoroughly enjoying your blog. I as well am an aspiring blog writer
    but I’m still new to everything. Do you have any suggestions for rookie blog writers?
    I’d genuinely appreciate it.

  11. I do not know whether it’s just me or if perhaps everyone else encountering
    issues with your website. It seems like some of the written text in your posts are running off the screen. Can somebody
    else please provide feedback and let me know if this is happening
    to them as well? This might be a problem with my browser because I’ve had this happen before.
    Thanks

  12. Hello there! This post could not be written any better!
    Looking through this article reminds me of my previous
    roommate! He continually kept talking about this. I most certainly will forward
    this post to him. Pretty sure he’s going to
    have a very good read. Many thanks for sharing!

  13. I’m not sure why but this site is loading incredibly
    slow for me. Is anyone else having this problem or is
    it a problem on my end? I’ll check back later on and
    see if the problem still exists.

Leave a Reply

Your email address will not be published.

*