Home › Forums › ACF PRO › ACF and WP Search › Reply To: ACF and WP Search
You may need to use setup_postdata() and wp_reset_postdata() along with the global $post variable described here https://www.advancedcustomfields.com/resources/post-object/
add_action('acf/save_post', 'copy_persons_to_people', 20); // priority 20 runs after acf has saved content
function copy_persons_to_people($post_id) {
// we need to create a new meta key to hold the list of people
// this is the meta key you'll use to set up the search of this field
$meta_key = 'related_peoples';
// clear this field if it exists for this post
// need to start fresh each time
delete_post_meta($post_id, $meta_key);
// see if there are any related people
$persons = get_field('team_members');
if ($persons) {
global $post;
foreach ($persons as $post) { // need to use $post here
setup_postdata($post);
add_post_meta($post_id, $meta_key, $post->post_title, false);
}
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.