Home › Forums › Backend Issues (wp-admin) › Issue with rest api and new acf fields on old posts
I stumbled upon an issue while creating new acf-fields for my wooC products which I use rest-api to fill in the values like this:
using wc/v3/products
"meta_data" : [ {
"key" : "acffield",
"value" : 99.99
}]
This has worked flawlessly, but the other day I created a couple of new fields and then the values only showed in the backend. On the frontend the value only showed up on the newly created products – not the old ones.
After troubleshooting some I found out that on the products that worked looked like this in my get response:
{
"id": 4748103,
"key": "acffield",
"value": 99.99
},
{
"id": 4748104,
"key": "_acffield",
"value": "field_5e218ed8dc049"
},
While products that don’t work is missing the protected _acffield entry in meta_data.
The issue is fixed with manually updating the product backend. Quick edit and then update does not work.
I’m guessing this is just how acf works – storing the values in their own tables and they are only being created upon save/update in the backend. But do you have any clever way for me to update all my products without manually editing 2000k products? And how to prevent this from happening the next time I add new fields?
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 😉
If anyone has this problem – the answer in this post might prevent this from happening (untested in my scenario): Headless WordPress: ACF fields return empty once a post is created with WP-API
The topic ‘Issue with rest api and new acf fields on old 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.