Hello
Hope somebody can help – I’d like to copy the content of the regular WP content area into a acf textfield.
I’ve put the following code into functions.php but it doesn’t work – what am I missing?
add_action( 'init', 'copy_data' );
function copy_data() {
if ( !is_admin() )
return;
// WP_Query arguments
$args = array (
'post_type' => array( 'ze_event' ),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
update_field( 'field_56b4d48d4b471', get_the_content(), get_the_ID() );
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
}
First step, you’re in a function so you need to declare the global $post.
If that does not work the add the post id to the function calls instead of letting WP try to figure them out. I’d probably do this anyway.
add_action( 'init', 'copy_data' );
function copy_data() {
if ( !is_admin() )
return;
// declare global, you cant access $post without it
global $post;
// WP_Query arguments
$args = array (
'post_type' => array( 'ze_event' ),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
update_field( 'field_56b4d48d4b471', get_the_content($post->ID), $post->ID );
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
}
Thanks for this. Any idea why it works for posts that I create, but not for posts that I import (using the standard WordPress import/export)?
I’m guessing that it’s somehow related to the fact that the field I am trying to copy into doesn’t exist in the original post before export.