Ready to get started?Download WordPress


Mass import data into custom fields (11 posts)

  1. moseymosey
    Posted 7 years ago #

    I've been searching around, but there doesn't seem to be much info about mass importing data into the custom fields of posts, so was wondering where would be the best place to start/start looking/reference?


  2. MichaelH
    Posted 7 years ago #

    Not that I have an answer, but interested in exactly what you want to achieve.

    Will each post have a different set of Custom Field keys and values, or, will there be a bunch of Custom Field keys and values that get imported to each and every post?

  3. moseymosey
    Posted 7 years ago #

    MichaelH: Thanks for your reply :) It does help me to clarify the situation:

    Half of the posts won't use Custom Fields at all, and the other half will have the same (or roughly similar) Custom Field keys, but different values.
    (I hope I've understood the definition correctly!)

    It's a site for a sports club, where about half the posts are news posts. The other half will be results for each fixture, which will have the same sort of Custom Field keys (e.g. Score, Venue) but different values?

    I hope I've explained it better this time!

    Thanks :)

  4. syncbox
    Posted 7 years ago #

    I think you could insert a few custom fields (maybe one of each type) and then take a look at the database to see how these are entered.

    From there, you could use SQL to import a CSV file into the database.

    I think that this would be useful, too. I use the c2c_get_custom() plugin all the time and my last use included a glossary of legal terms... WHAT a PITA to enter all 300-400 entries!

    It would be nice if the plugin author found a way to do this for that plugin. You'd have to be comfortable with db manipulation using SQL insert statements otherwise...

  5. moseymosey
    Posted 7 years ago #

    syncbox: Thanks for your reply! :) I do find it helpful!

    With regards to CSV: I've just done a search 'importing via csv' and it threw up this result: http://wordpress.org/support/topic/80109 - which I think is something I might be able to manage, since alot of the data is easily exportable as CSV rather than RSS.

    The customfield plugin you described also sounds useful too. I've come across something similar whilst searching for custom field related queries, which was called 'Enzymes'. (http://www.mondotondo.com/aercolino/noteslog/?page_id=77) I'm not sure which I will use at the moment, but as you've explained that the c2c_get_custom plugin can handle LOTS of data!

    I shall try and investigate the customfield entry in the SQL as you've suggested, and try and see if there is a good format to use in the meantime. It will be a bit of a learning curve since I only know basic SQL.

    p/s: I'm also following another plugin for a custom fields posting interface: http://rhymedcode.net/projects/custom-write-panel

  6. MichaelH
    Posted 7 years ago #

    Here's the sql statement to insert one Custom Key and it's value in the wp_postmeta table. In this case the post has post_id = 1.

    INSERT INTO wp_postmeta ('meta_id', 'post_id', 'meta_key', 'meta_value') VALUES (NULL, '1', 'key3', 'key3value');

    If your $table_prefix in wp-config.php is not 'wp_' then use that value in front of postmeta.

    It is a brute force approach but you could use a spreadsheet program (or text editor) to build all the necessary INSERT statements. Of course that's assuming this is a one time thing.

    Alsl, it would seem your imported data is going to need the post id to which that data applies.

  7. moseymosey
    Posted 7 years ago #

    MichaelH: Thanks again for your reply! :) the SQL insert statement is really useful! I think I will probably be using a spreadsheet program (Excel) since it turns out I will have to merge/compare two csv files first before importing the data (thankfully a one-time) separately - first the main post data, and then the custom field part.

    This is a a newbie question: but is it possible to set post-ids when importing e.g. from csv?

    Thanks! :)

  8. ccoupe
    Posted 7 years ago #

    INSERT creates a new post-id automatically. but you can set one IF IT'S not already in use. If it is you use the UPDATE sql statement.

    In the specific case of "sports scores" reporting , I'd think twice against stuffing it into wordpress's. tables and instead create your own DB table or two or three. You'll end up learning just as much SQL and PHP either way to get working but far less risk involved if WP changes or the plugin changes or (gasp) fails to change when needed.

  9. MichaelH
    Posted 7 years ago #

    Yes, mosey, that's the key question, what post id to use for each and every custom fields. Because the post id already exists, correct? So you will need to use the appropriate post id on each insert.

  10. moseymosey
    Posted 7 years ago #

    Apologies for the late reply, I've been away for the weekend.

    ccoupe: Thanks for the tip. I think I will consider it for the next major update of the site! I hadn't actually thought of the possibilities of WP/plugin changes. For the moment, I think I may still have to stick with custom fields, as the site will be maintained by less-technical users, and the CWP will give them an easy admin interface to use for the time being.

    MichaelH: Thanks again! The post-ids do exist at the moment, but I'm willing to start afresh with a new install of WP in order to set the new post-ids, since I think that will be easier than looking up the ids of each post!

  11. encyclomoto
    Posted 6 years ago #

    I had the same problem and modified an existing class to do this. I made a post about it on my blog. You can get the file, sample csv and instructions. Works on 2.0+


Topic Closed

This topic has been closed to new replies.

About this Topic