Home › Forums › Backend Issues (wp-admin) › Issue with rest api and new acf fields on old posts › Reply To: Issue with rest api and new acf fields on old posts
Ok, so I found a way to save/update a little bit quicker with update_field();.
Basically made a loop with pagination (because getting 2000k++ products is a bit heavy) and fetched the value needed from get_post_meta(); and used update_field(); to save the value correctly.
Here’s my code (got some var_dumps just for testing and such).
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product', /* product post type */
'posts_per_page' => 99, /* all products */
'paged' => $paged
);
$products = new WP_Query( $args );
// check if products exists
if( $products->have_posts() ) {
?>
<div class="pagination">
<?php
//Pagination
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $products->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
<?php
// loop products
while( $products->have_posts() ): $products->the_post();
$postid = $post->ID;
echo "<li>";
//Get values stored in meta
$sor = get_post_meta($postid, 'prisgruppe_sor_pris', true);
$nord = get_post_meta($postid, 'prisgruppe_nord_pris', true);
//My acf-fields are in a group, so here i make an array for the group subfields
$valuesSor = array(
'pris' => $sor
);
$valuesNord = array(
'pris' => $nord
);
//update fields which are groups
update_field( 'prisgruppe_nord', $valuesNord, $postid );
update_field( 'prisgruppe_sor', $valuesSor, $postid );
//testing things
var_dump(get_field('prisgruppe_sor'));
var_dump($postid);
echo "</li>";
endwhile;
// reset query to default query
wp_reset_postdata();
}
Would still love to hear if anyone have a solution to prevent this mess 😉
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.