Support

Account

Home Forums General Issues Can related posts inherit ACF field values?

Solved

Can related posts inherit ACF field values?

  • I am newer to ACF but can’t seem to find out if this is possible. Is it possible for related posts to inherit specific ACF field values?

    For example, I am trying to create a card game database where the base card has multiple variants. So one Base Card 1 will have Variant 1, 2, 3, etc. So I have a Field Group for Base Cards that includes things like Power and Cost. Is it possible for the values in the Base Card for Power and Cost to be inherited in Variant 1, 2, and 3? So for example, if I put Power 1 in Base Card 1, then Variants 1, 2, and 3 would all inherit that value of 1 in their Power field? Then, if I update the Power value to 2, each of the Variants will update.

    I’ve seen answers about global fields in the ACF Pro Options settings, but I’m going to have 300+ Base Cards, so I’m not sure that I want to create that many global variables. I know I could bulk edit too, but I’m trying to figure out a way to link fields between posts so the values sync. I already have a relationship between the Base Card and each Variant.

    I have searched documentation, videos, and this forum, but haven’t found what I am looking for. Maybe I’m using the wrong search terms. Thanks for any assistance you can give me!

  • If you have the posts linked already I think you’d just need to update the field in the relationship posts when the post is saved.

    I used update_field() and ACF/save_post

    I’ve done something similar in a solution I built to update the field values of a specific post from any page on the website.

    
    function update_variant_fields($post_id) {
    
      // Get related Base Cards using the relationship field
      $base_cards = get_field('base_card_relationship_field', $post_id);
    
      if ($base_cards) {
        foreach ($base_cards as $base_card_post) {
          $power = get_field('power', $post_id);
          $cost = get_field('cost', $post_id);
    
          update_field('power', $power, $base_card_post->ID);
          update_field('cost', $cost, $base_card_post->ID);
        }
      }
    }
    add_action('acf/save_post', 'update_variant_fields'); // Update on post save
    

    I realized after I made it could have probably changed $base_card_post->ID into its own variable.

    Hope this helps!

  • Thank you, Andrew!

    Sorry for the much delayed response. I just got around to looking at this website again and your suggestion helped me figure out what to do and I just got it working this morning. Here is what I ended up using:

    function update_variant_fields($post_id) {
      // Get related Variant Cards using the relationship field
      $variant_cards = get_field('variants', $post_id);
    
      if ($variant_cards) {
        foreach ($variant_cards as $variant_card_post) {
          $base_power = get_field('power', $post_id);
          $base_cost = get_field('cost', $post_id);
    
          update_field('power', $base_power, $variant_card_post->ID);
          update_field('cost', $base_cost, $variant_card_post->ID);
        }
      }
    }
    add_action('acf/save_post', 'update_variant_fields'); // Update on post save

    Thanks so much for the direction!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.