I need help converting a Wordpress Blog into Joomla

Hi Everyone,

I need to import a Wordress Blog with around 1000 posts and I'm using component Jconverter that can be found here: http://ow.ly/WbuL , this component works only in half, I explain. It does import posts and categories, I'm not a developer but I went into the code and fixed the posts import, they were imported without alias so it's fixed now, but when posts are imported they lose the category they belong to.

I took a look at the database structure of wordpress, that can be found here: http://codex.wordpress.org/File:WP_27_dbsERD.png , to try to find a way link the posts to their category while importing but with no success, I don't know either Mysql, at least not enough to make a query that would keep this relationship between the post and its category.

Here is the code used to import the posts (from jconverter component):

function migrateWpPosts() {

$query = "SELECT * FROM " . $this->_config['prefix'] . "posts WHERE post_status = 'publish' AND post_type = 'post'";
//echo $query;

$this->_externalDB->setQuery( $query );
$posts = $this->_externalDB->loadObjectList();
//print_r($posts);
//$ret = $this->insertObjectList('#__users', $users);

$db =& JFactory::getDBO();
$query = "SELECT id FROM #__sections WHERE title = 'WordPress'";
$db->setQuery( $query );
$sid = $db->loadResult();


foreach ($posts as $post)
{
$pTitle = mysql_real_escape_string($post->post_title);
$pName = mysql_real_escape_string($post->post_name);
$pIntro = mysql_real_escape_string($post->post_excerpt);
$pContent = mysql_real_escape_string($post->post_content);
$pDate = mysql_real_escape_string($post->post_date);
$query = "INSERT INTO #__content (title, alias, introtext, #__content.fulltext, created, created_by, state, sectionid) VALUES ('$pTitle', '$pName', '$pIntro', '$pContent', '$pDate', 62, 1, '$sid')";
//echo $query;
$db->setQuery( $query );
$ret = $db->query();
//echo $db->errorMsg();
}

return $ret;

}

Does anyone have a suggestion on how I could achieve this query?

Thank you very much for your help.

Views: 139

Tags: Convert, Joomla, Mysql, PHP, Query, Wordpress

Comment by Gobezu Sewu on January 15, 2010 at 9:32pm
you have probably tried this already, but let me still suggest the followings i just googled (but never tried) this, hope it works
further @joomlaworks: k2 should have one such import wordpress export file option, which would be a neat and smooth way to transit to k2 and joomla
Comment by Helio Alves on January 16, 2010 at 7:10am
Hi Gobezy, thank you for your reply.

I tried that but unfortunately didn't work, I keep getting errors and even though I get a sql file I can't import it I get errors and impossible to import. I'll keep trying to make this code to work :)

I will give a try to K2, it seems a very interesting CCK.

Thank you again :)
Comment by Amy Stephen on January 16, 2010 at 10:18am
Helio -

Your query looks very close. You are missing the Category (catid) value. If you can figure out how to determine that value for your INSERT statement, you might have what you need.

There are a number of important data relationships that must be established in Joomla! to correctly import data. Joomla! has Sections - and Categories belong to Sections - and Articles belong to Categories. So, in your INSERT statement, you have to provide a valid sectionid value and you must provide a valid catid value. That catid has to belong to the sectionid.

jos_sections id column must match jos_content sectionid.

jos_categories id column must match jos_content catid *and* the section column in the jos_categories table must match the jos_content sectionid.

You also need to make certain that the created_by value matches a jos_user primary key (id).

If it's too complicated to create valid relationships before inserting the data, you can create 1 section, 1 category, 1 user. Look up the values, and hard code those values in your INSERT statement, like follows (assumes userid of 66, sectionid of 1, catid of 1:

$query = "INSERT INTO #__content (title, alias, introtext, #__content.fulltext, created, created_by, state, sectionid, catid) VALUES ('$pTitle', '$pName', '$pIntro', '$pContent', '$pDate', 66, 1, 1, 1)";

That's an easy way to import the data. Then, you can use the Joomla! Administrator, and it's User Manager, Section Manager, and Category Manager to create the values you need. Then, use the Article Manager to move things into the right Section/Categories.

Hope some of that helps, but, let me know what you think or if you have questions.
Comment by Helio Alves on January 17, 2010 at 11:34am
Hi Amy,

Thank you very much for your help.

Actually my problem is the following, I hope I can clearly explain.
I have two problems, first is when I import categories, here is an example:
In wp_terms, term_id are 3, 7, 8 if I import them the first time in jos_categories no problem but if I import them into jos_categories where I added and deleted entries the ID will increment and will be some thing like his 13, 17, 18 since field ID in the jos_categories is autoincrement.

Second is when I import posts. When I import posts everything is OK but catid, since the category that the post belongs to is in the wp_terms table and the way it is linked to the post is by post_id and term_id and in the middle we have wp_term_relationships and wp_term_taxonomy to allow many to many.

I don't know how to build a query that would get post fields and the category it belongs to, I think I would need to use a left or right join, I don't know which of them, but with all these tables I have no idea how to achieve this.

This is what I tried to do, with o success I must say:
$query = "SELECT * FROM " . $this->_config['prefix'] . "posts WHERE post_status = 'publish' AND post_type = 'post'";
$query_cat = "SELECT * FROM" . $this->_config['prefix'] . "terms";
//echo $query;

$this->_externalDB->setQuery( $query, $query_cat);
$posts = $this->_externalDB->loadObjectList();


First I don't know if I can make two queries and concatenate them like I did here:
$this->_externalDB->setQuery( $query, $query_cat);

Then I do the follwoing:
foreach ($posts as $post)
{
$pID = mysql_real_escape_string($post->id);
$pTitle = mysql_real_escape_string($post->post_title);
$pName = mysql_real_escape_string($post->post_name);
$pContent = mysql_real_escape_string($post->post_content);
$pDate = mysql_real_escape_string($post->post_date);

I adde the line bellow:
$catID = mysql_real_escape_string($post->term_id);
I though this would take the term_id from the table wp_terms but it doesn't, the field is empty
$query = "INSERT INTO #__content (title, alias, introtext, created, created_by, state, sectionid, catid) VALUES ('$pTitle', '$pName', '$pContent', '$pDate', 62, 1, '$sid', '$catID')";
echo $query;
$db->setQuery( $query );
$ret = $db->query();
echo $db->errorMsg();
}

So my biggest issues is getting the ID of the category the post belongs to, does any one know how I could do a query that would retrieve this ID?

Thank you very much for your PRECIOUS help :)
Comment by Amy Stephen on January 17, 2010 at 12:51pm
Would it be possible to email me your file? AmyStephen AT gmail DOT com.

Thanks!
Comment by Helio Alves on January 20, 2010 at 7:38pm
Hi Amy,

I'd like to know if you got the file and if you got the chance to take a look. I've made many research about this issue but with no success. I wasn't able to find anything that could help me, the worst is that I'm pretty sure I know what the problem is but I don' know how to achieve it.

Thank you
Comment by Helio Alves on January 21, 2010 at 11:57am
Ok, I think I found some thing that could make things go a step further.
Here: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Q...
I found a query that allows me to do what I need, at least I believe so.

Here's the query:
SELECT wposts.*
FROM $wpdb->posts wposts
LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE wpostmeta.meta_key = 'customDateField'
AND wpostmeta.meta_value >= CURDATE()
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id IN(1,2)

So I tried to make this query work on my component, with no success, but I think I'm pretty close.
Here's my query:
$query = "SELECT wposts.*
FROM wp_posts wposts
LEFT JOIN wp_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN wp_term_relationships ON (wposts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.term_id IN(1,2,3,4,5,6,7,8";

I think this will work if I know how to tweak this query.
Does anyone is good enough with Mysql to tell me what I'm doing wrong?

Thank you
Comment by Helio Alves on January 22, 2010 at 10:00am
OK,

So here's the query to work, well mostly.

$query = "SELECT wposts.*, wp_term_taxonomy.*
FROM wp_posts wposts
LEFT JOIN wp_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN wp_term_relationships ON (wposts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE wp_term_taxonomy.taxonomy = 'category'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'";


This query select indeed all the published posts into the Wordpress database with its category, the only problem is that, for some reason, it selects all the posts twice.

Does any one has any idea on where could be the problem?
Comment by Helio Alves on January 22, 2010 at 10:09am
OK,

So here's the query to work, well mostly.

$query = "SELECT wposts.*, wp_term_taxonomy.*
FROM wp_posts wposts
LEFT JOIN wp_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN wp_term_relationships ON (wposts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE wp_term_taxonomy.taxonomy = 'category'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'";

This query select indeed all the published posts into the Wordpress database with its category, the only problem is that, for some reason, it selects all the posts twice.

Does any one has any idea on where could be the problem?
Comment by Amy Stephen on January 22, 2010 at 10:12am
Try adding DISTINCT (SELECT DISTINCT wposts.* etc")

Sorry for not getting back to you!

Comment

You need to be a member of All Together, As A Whole to add comments!

Join All Together, As A Whole

Badge

Loading…

© 2012   Created by Amy Stephen.

Badges  |  Report an Issue  |  Terms of Service