Home › Forums › Backend Issues (wp-admin) › Adding new field to existing posts › Reply To: Adding new field to existing posts
Check this out:
$args = array(
'post_type' => 'product', /* product post type */
'posts_per_page' => -1 /* all products */
);
$products = new WP_Query( $args );
// check if products exists
if( $products->have_posts() ) {
// loop products
while( $products->have_posts() ): $products->the_post();
// check if field exists
if( !get_field( 'field_slug' ) ) {
update_field( 'field_slug' ); /* update field */
}
endwhile;
// reset query to default query
wp_reset_postdata();
}
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.