Moving field values (WordPress export files) and duplicating posts
5 min read
Field values live in the field group tables, not in wp_postmeta, so a normal WordPress export file would leave them behind. Mass Custom Fields adds them to the export and reads them back on import. It also copies them when another plugin duplicates a post.
Nothing needs to be switched on: both work as soon as the plugin is active.
Export posts with their values (WXR)
- On the source site open Tools › Export (the WordPress screen, not WPM Fields › Tools).
- Choose All content, or Posts, Pages or a custom post type, then Download Export File.
- In the file, every post that has saved field values carries one extra custom field:
<wp:postmeta>
<wp:meta_key><![CDATA[_wpmcf_fields]]></wp:meta_key>
<wp:meta_value><![CDATA[{"book_details":{"isbn":"978-3-16-148410-0","pages":320}}]]></wp:meta_value>
</wp:postmeta>The value is JSON: the field group key, then each field key with its value. Posts without values get no extra entry.
Good to know:
- Values of posts, pages and custom post types are exported. Values of terms, users, comments and options pages are not part of a WXR file; copy those with your own script (
wpmcf_get_field()andwpmcf_update_field()). - Password fields are exported as they are stored. Treat the export file as sensitive.
- Plugins that build WXR files without WordPress's own exporter (for example some command-line exporters) may not include the values. Open the file and search for
_wpmcf_fieldsto check. - The file is a normal WXR file. Importing it on a site without Mass Custom Fields simply stores
_wpmcf_fieldsas a custom field.
Import posts with their values
Do these steps in this order on the target site:
- Install Mass Custom Fields and import the definitions first: the field groups must exist with the same keys. Use Export and import (JSON) or Local JSON. Post types and taxonomies the posts belong to must exist too.
- Open Tools › Import, choose WordPress (install the WordPress Importer when asked), upload the file and run it as usual.
wp importand other importers that save custom fields through WordPress's own functions work the same way. - Open a few imported posts and check the fields.
While an importer runs, each _wpmcf_fields entry is caught before WordPress stores it as a custom field:
- the values are checked by their field types, exactly like values typed in the editor (tags are stripped from plain text, numbers must be numbers, choices must exist, and so on);
- the cleaned values are written to the new post's row in the field group tables;
- nothing is stored in
wp_postmetafor them, so your postmeta stays clean.
What the importer skips silently: groups that do not exist on the target, fields that do not exist in a group, values the field type refuses, and fields whose type is not available on the target's plan.
During an import the groups' location rules are not checked: a value of a group that is placed on pages is written even if the imported post is a regular post. Outside an import the entry is applied only when the current user may edit the post and the group is located on it.
Known limit: ids are not translated
Values that point to other content (relationship, post object, page link, taxonomy, user, image, file, gallery) are written with the ids they had on the source site. WordPress usually gives imported posts and media new ids, so:
- an id that does not exist on the target is dropped by the field type;
- an id that happens to exist can point to the wrong item.
After importing, re-select those fields, or fix them with a short script using wpmcf_update_field().
Duplicate a post and keep its values
Duplicating plugins copy custom fields (wp_postmeta) only, so without help the copy would have empty fields.
Yoast Duplicate Post
When Duplicate Post (by Yoast) is active, nothing needs configuring. The field rows follow the copy when you use:
- Clone and New Draft;
- Rewrite & Republish: the changes of the rewritten copy are merged back into the original post, including their field values.
This is switched on automatically as the integration.duplicate-post integration. If it keeps failing it switches itself off and the reason is listed under Settings › Health (see Health and troubleshooting).
Other duplicators and your own code
Any code that creates a copy of a post can copy the values with one action, after the new post exists:
// $source and $copy are post ids.
do_action( 'wpmcf/fields/copy_object', $source, $copy );Behaviour:
- All field groups with a row for the source post are copied, one database query per group, including the extra columns of fields (such as map latitude and longitude).
- A value already stored for the copy in those columns is overwritten.
- The same source-and-copy pair is copied once per request; a second call in the same request does nothing.
- A group that fails is logged, and the other groups are still copied.
Duplicating a post never affects the original.
Troubleshooting
Imported posts have empty fields. Check, in order: the field groups exist on the target with the same keys; the groups have a location (a group without location has no table rows); the export file contains _wpmcf_fields; the field types of those groups are available on the target's plan.
My relationship fields point to the wrong posts. See Known limit: ids are not translated above.
The duplicate has empty fields. The duplicating plugin is not Yoast Duplicate Post and does not call the action. Ask its author to call wpmcf/fields/copy_object, or hook your own code to the plugin's "post duplicated" action and call it there.
Still stuck? Contact support