Flushdrew
Flushdrew

Reputation: 133

Import Db field as wordpress custom field

I have an old site and i've import most of it in a new wordpress project. The point is i had more field for my post in my old site than in wordpress. So i created custom field in my post type and now i would like to import some data from my old website directly in my wordpress DB. I kept the same ID for the post in my old and new project. So how can i automaticaly import this data please ?

I have two field to import in the new two i've created

Thank you

EDIT :

I tried this kind of SQL request but it didn't work

    Insert INTO wp_postmeta ( post_id , meta_key , meta_value)
SELECT 
    prod_id,
    prod_auteur
WHERE 
    meta_key = "Auteur(s)"
FROM
    produit

EDIT : The right request was :

Insert INTO wp_postmeta ( post_id, meta_key, meta_value) SELECT prod_id, 'Auteur(s)', prod_auteur FROM produit

Upvotes: 0

Views: 60

Answers (2)

Flushdrew
Flushdrew

Reputation: 133

Insert INTO wp_postmeta ( post_id, meta_key, meta_value) SELECT prod_id, 'Auteur(s)', prod_auteur FROM produit

Upvotes: -1

Codeartist
Codeartist

Reputation: 793

If the ID's of the old sql data is the same as your post's ids, then you can do it something like that:

function your_function() {
    //grab your SQL data here
    while(<your sql query have rows>) {
        $pid = //id data from query
        $val1 = //first value to be imported
        $val2 = //second value to be imported
        if ( empty( get_post_meta($pid, 'first_value_name', true) ) ) update_post_meta($pid, 'first_value_name', $val1);
        if ( empty( get_post_meta($pid, 'second_value_name', true) ) ) update_post_meta($pid, 'second_value_name', $val2);
    }
}
add_action( 'wp_footer', 'your_function' );

You put it inside your functions.php, run your site couple of times & remove it. After that all data should be in your post's meta. Note, that you should do all of the necessary things like connecting to the DB, retrieving the query etc. Also, if your old data is quite big, you may want to chunk it somehow.

Upvotes: 0

Related Questions