Home › Forums › General Issues › How can i update my subfields › Reply To: How can i update my subfields
Thanks, yes i tried this approach and it doesn’t work for me. I updated these fields with update_post_meta
but now i can see these fields values only on post edit page. And function get_fields
cant get their values. If i click update post then function works correctly. But i cant update all the posts using this approach, there are about 8000 posts.
I used this code to update the fields
function some_field_update(){
$my_posts = new WP_Query;
$myposts = $my_posts->query( array(
'post_type' => 'product',
'posts_per_page' => 10000,
) );
foreach( $myposts as $pst ){
if ($pst){
update_post_meta($pst->ID, 'main_specifications_colors', get_post_meta($pst->ID, 'colors')[0]);
}
}
}
add_action('init', 'some_field_update');
And this code i use to display the values
$fields = get_fields($product_id);
?>
<?php if( $fields ): ?>
<?php foreach( $fields as $name => $value ):
$field = get_field($name, $product_id);
if (array_filter($field)){
echo '<h2>'.ucwords(str_replace('_', ' ', $name)).':</h2>';
echo '<table>';
foreach ($field as $key => $val){
if($val){
echo '<tr><td>'. ucwords(str_replace('_', ' ', $key)).': </td>';
echo '<td>'. ucwords(str_replace('_', ' ', $val)).'</td></tr>';
}
}
echo '</table>';
}
?>
<?php endforeach; ?>
<?php endif;
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.