Home › Forums › General Issues › Using “update_field” to update a group
Apologies if what I’m asking about is ACF 101. I’m kind of new when it comes to programmatically updating field values. My problem concerns a site that is using ACF Pro (version 6.3.3). I have a custom post type called “Projects” that is registered through theme code. I’ve created over 100 projects, each with their own featured image (using the built-in WP featured image field). Now, the client wants the ability to have multiple featured images, one for each “discipline” (a custom taxonomy I created for them). There are three disciplines, one of which is called “Architecture”. They want to show a different featured image for each project on the frontend, depending on which discipline term has been selected by the user. To that end, I have created a group called “discipline_imgs” with 3 image sub-fields. Each sub-field’s field name corresponds to a discipline term slug. For example, there’s an image sub-field labelled “Architecture” with the field name “architecture”. Each image sub-field is set to return the image ID.
They want to copy the current featured image in each project to the “architecture” image sub-field, and then they want the other sub-fields updated with different images. So, rather than going through all 115 projects and manually copying the featured image over, I decided to write a script to do it. The script is in the form of a theme page template. Here is the template code:
<?php
/**
* Template Name: Copy Project Featured Image to Architecture Image
*/
defined('ABSPATH') || die(1);
is_super_admin() || die(1);
header('Content-Type: text/plain; charset=' . get_bloginfo('charset'));
query_posts(
[
'post_type' => 'project',
'posts_per_page' => -1,
],
);
$project_count = $project_updated_count = 0;
while (have_posts()) :
the_post();
++$project_count;
[
'architecture' => $architecture_img,
] = (array)get_field('discipline_imgs');
var_dump(compact('architecture_img'));
if (0 < $architecture_img) :
continue;
endif;
$featured_img = get_post_thumbnail_id();
var_dump(compact('featured_img'));
if (!$featured_img) :
continue;
endif;
$result = update_field(
'discipline_imgs',
['architecture' => $featured_img],
);
if ($result) {
++$project_updated_count;
}
endwhile;
echo sprintf(
'Updated %d of %d project(s).',
$project_updated_count,
$project_count,
) . "\n";
I created a page and set the template to this template. When I visit the page, it spits out a bunch of var_dump()
output, so that I know that each project has a value of NULL
for the “architecture” sub-field and a non-zero value for the featured image, as it should be. Then at the end it says “Updated 0 of 115 project(s)”. So something is wrong with the update_field()
lines and the projects aren’t getting updated. Thoughts?
You must be logged in to reply to this topic.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.