Custom Fields on Chapter CPT won't export

Hello Pressbooks community. Well I’m currently developing a plugin that uses custom fields in the Chapter CPT of Pressbooks. The problem comes when ever i export/import the book data. When i try import it in a clean site install using just Pressbooks and my plugin, i don’t get the data to show in my custom fields. If someone knows what might be the problem or if you have any clue i would be very happy to hear from you.
BTW i added custom fields in the Book Info section of Pressbooks and this data seems to export and import as expected.
All the above were tested on:
Pressbooks 3.9.9 and 3.9.10
This is the way i use to add metaboxes and metafields:


Thank you!

What format are you exporting / importing?

If you are trying to export/import using WXR then you need to prefix your new custom fields with pb_

Code:

foreach ( $p['postmeta'] as $meta ) {
    if ( 0 === strpos( $meta['key'], 'pb_' ) ) {
        // ...

> Link to code <

Hope this helps.

Reading again, can you confirm you are adding custom fields to chapters? Not the metadata post?

My answer only applies if you are adding to Book Info. Import for anything else .you will just have to add your fields the filter:

$meta_to_update = apply_filters( 'pb_import_metakeys', array( 'pb_section_author', 'pb_section_license', 'pb_short_title', 'pb_subtitle', 'pb_show_title', 'pb_export' ) );

> Link to code <

Hey! Thank you for the response,I’m actually adding new fields on the chapter CPT only. So i guess the second answer should help me.
Thank you!

Do i also need to do something on the exporting function or just modify the PB Core code you suggested?
Thank you!

Just modify the filter in your plugin? Example:

add_filter( 'pb_import_metakeys', function ($a) {
    $a[] = 'your_field_1';
    $a[] = 'your_field_2';
    // ...
    return $a;
});

1 Like