Support

Account

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();
      }
    }