Home › Forums › Backend Issues (wp-admin) › ACF Update multiple fields and multiple posts
I am trying to copy content from old flexible layouts to new flexible layouts. For example, I have two flexible layouts called image media and one called video media. They each have multiple fields such as gallery, title, description, etc. I created new a new media layouts within in the flexible content that I would like to copy all of this content over on several pages.
I would like to:
I have a successful PHP function that is however unreliable and I’m not sure if what I’m doing is the best way of doing what I need done. I have copied the function below, but a general summary of what I’m doing:
global $wpdb
and get_posts()
array
for each page storing this dataupdate_field()
to update 67 pagesadd_action( 'acf/init', 'update_acf_image_section' )
The issue I’m having is that is sometimes creates duplicate layout rows and I’m not confident enough in this function to push to production.
Here is a sample of my code:
add_action( 'acf/init', 'update_acf_image_section' ); function update_acf_image_section() { $pages = get_acf_template( 'template-name', 67 ); // this uses get_posts(); $procedure_layout_key = 'field_KEY'; foreach ( $pages as $page ) { $loop = 0; $i = 0; $layouts_data = get_field_object( $procedure_layout_key, $page->ID ); $image_values = false; $image_galleries = []; if ( have_rows( 'procedure_child_layout', $page->ID ) ) : while ( have_rows( 'procedure_child_layout', $page->ID ) ) : the_row(); if ( 'procedure_child_media_gallery' === get_row_layout() ) { $type = get_sub_field( 'media_type', $page->ID ); if ( 'image' === $type && 0 === $loop ) { if ( get_sub_field( 'title', $page->ID ) ) { $image_title = get_sub_field( 'title', $page->ID ); } .... $image_values = array( 'acf_fc_layout' => 'image_gallery', 'image_gallery_title' => $image_title, 'image_gallery_description' => $image_descript, 'image_gallery_button_link' => $image_button_text, 'image_gallery_button_text' => $image_button_link, 'image_disclaimer_text' => $image_disclaimer, 'image_galleries' => $image_galleries, ); $row = $i + 1; } } $i++; endwhile; endif; if ( $image_values && 0 ) { get_acf_values(...); // this uses acf update_field() } } }
The topic ‘ACF Update multiple fields and multiple posts’ is closed to new replies.
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.