Not really. Most of my ACF data for that site was in a single field. I added code to copy that field specifically, like:
$layout = get_field_object('content_rows', $post_id);
...
update_field('content_rows', $layout['value'], $inserted_post_id);
That doesn’t seem to work. If I dump the $value variable before the line
upate_post_meta($inserted_post_id, $key, $value);
It’s the key of the field, not the field structure/array. But if I try to pass the $field as the last argument of update_post_meta()
, it doesn’t work.
I figured out a decent solution using get_field_object and update_field.
For anyone passing this way, you want to pass the value of the [‘value’] index of the retrieved object to update_field.
$layout = get_field_object('field_name', $post_id);
...
update_field('field_name', $layout['value'], $inserted_post_id);
I’d love to have code that can iterate through fields w/o my having to hard-code the field name, though.
I was struggling with this this morning and came across this thread. I just wanted to confirm that I was able to fix this by spinning up a temporary install of WordPress. I grabbed the old version of ACF from the site I was exporting from. Did an old-style export/import that uses the WordPress importer XML. Then I installed ACF Pro, did the database upgrade, then exported the JSON from the temp install. Imported that into my new site using the ACF JSON importer and all was well.
I recognize that this is an old thread, but wanted to add something that I constructed using this info that I found handy. I used ACF to add a true/false field to user forms. I called it “show_custom_fields_admin”. Then I use the above code in conjunction with the value of that true/false field to hide or display the ACF menu.
function remove_acf_menu(){
global $current_user;
$show = get_field('show_custom_fields_admin',$current_user);
if (!$show){
remove_menu_page( 'edit.php?post_type=acf-field-group' );
}
}
add_action( 'admin_menu', 'remove_acf_menu', 100 );
When adding the true/false field to the user form, be sure to show it only if the user’s role is “admin”.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.