0

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.

1 Answer 1

0

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!';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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