Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created a custom post type with several custom fields with the help of WPAlchemy, and now I need to import a bunch of stuff from some CSV files.

I'm using WPALCHEMY_MODE_EXTRACT.

By toying with csv import plugins, I know I can import the fields correctly in the postmeta table, but the problem is that I can't see them in both back and front end.

I managed to isolate the difference between manually inserted posts and imported posts, and it's an extra meta field (_myprefix_meta_fields) that contains serialized stuff I don't know how to generate.

Any suggestion on how to solve the problem and correctly import the data?

UPDATE: Looking up WPAlchemy code (here) it seems that the only way to fix this is by running a script that for each post of custom type adds the extra meta field, that is just the serialization of the array containing all the metabox fields. Serializing and adding the meta field is done by using add_post_meta. I'll add myself a basic script as an answer when I'm done importing data.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Here's a piece of code similar to what I have used to fix the problem once imported the data:

<?php
define('MY_METABOX_ID', 'my_metabox_id');
define('MY_METABOX_PREFIX', 'my_metabox_prefix_');
define('MY_CUSTOM_POST_TYPE', 'my_custom_post_type');

// Include WordPress
define('WP_USE_THEMES', false);
define('WP_DEBUG', 1);
require_once('../wp-load.php');

// extract all MY_CUSTOM_POST_TYPE posts
$q = new WP_Query('post_type=' . MY_CUSTOM_POST_TYPE . '&nopaging=true');

// for each post
while($q->have_posts()) {
    $q->next_post();

    $p = $q->post;
    $id = $p->ID;

    // check if the field already exists, take action only if it does not
    if (!get_post_meta($id, MY_METABOX_ID . '_fields', true)) {

        // crete the content
        $fields = array(
            MY_METABOX_PREFIX . 'field1',
            MY_METABOX_PREFIX . 'field2',
            MY_METABOX_PREFIX . 'field3',
        );

        //add the meta field
        add_post_meta($id, MY_METABOX_ID . '_fields', $fields);
    }
}
echo 'DONE!';
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.