Home › Forums › ACF PRO › Force default value on existing posts › Reply To: Force default value on existing posts
Hi, thought I should drop my solution to future googlers.
My need was to create meta data for every post written by certain authors so I decided to have a function make that for me.
function function_run_once() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
);
$authors_to_update = array(x, y, z);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$post_id = get_the_ID();
$author_id = get_the_author_meta('ID');
if ( in_array( $author_id, $authors_to_update ) ) {
add_post_meta( $post_id, 'meta_data_name', meta_data_value, false );
}
endwhile;
wp_reset_postdata();
endif;
}
// Triggered once
add_action( 'init', 'my_run_only_once' );
function my_run_only_once() {
if ( did_action( 'init' ) >= 2 )
return;
if(!get_option('run_this_function_once')) {
function_run_once(); // Run the function
update_option('run_this_function_once', true);
}
}
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.