Support

Account

Home Forums ACF PRO Relationship field in a block

Solving

Relationship field in a block

  • Hi,

    Is it possible to pull data from a relationship field into a block?

    The block is created using ACF and functions fine on whole (ie it’s displaying on the front end). I simply can’t get it to include data from a relationship field.

    I have pages that are related and share similar info stored in ACF fields.

    This is what I’ve tried:

    $posts = get_field('relationship_field_name');
    if( $posts ) {
    foreach( $posts as $p) {
    
    $field = the_field('field_name', $p->ID); ?>
    
    <p><?php echo $field; ?></p>
        
    <?php }
    
    wp_reset_postdata();
    }

    I feel like I’m missing something fundamental! Any help much appreciated!

    Cheers
    Andy

  • @trenbania you’re using the_field('field_name', $p->ID) when you should be using get_field('field_name', $p->ID).

    Also, if you read the instructions for Relationship filed, you must call the foreach variable $post in order to override the global post variable. So your foreach loop should be: foreach($posts as $post). Then you have to setup the postdata: setup_postdata($post)

  • Thanks @kiwicreative but the docs say the_field('author', $p->ID); and foreach( $posts as $p ) as in there are two ways of doing it. I’ve tried both and neither work…

    Does that mean the docs are incorrect?

  • @kiwicreative sorry – I see what you mean – long day 🙂

    However, I AM using get_field to set the $posts variable. I just typed it wrong in my question above (which would have helped!).

  • So, just to clarify, I’ve updated the code in the block template and this is it, pasted straight from the site – no leeway for typos!

    $posts = get_field('info_from_growing');
    if( $posts ) {
    foreach( $posts as $post) { // variable must be called $post (IMPORTANT)
    setup_postdata($post);
    $scientificName = the_field('scientific_name');
    $family = the_field('family');
    $scientificTreeFamily = the_field('scientific_tree_family');
    // do something with the values
    }
    }
  • @trenbania the_field() directly prints the value…which is the same thing as doing echo get_field(). If you’re assigning the value to a variable, you must use get_field() Other than that everything looks fine to me.

  • @kiwicreative not sure if I have something more fundamentally wrong somewhere!

    I’ve changed it to get_field() and created a simple page template to test things. If I print_r($posts) I don’t see any custom field values in the array (not sure if I should), but the custom field values are empty on the page.

    Here’s my test page – https://treegrowing.tcv.org.uk/identify/testing-block – the three values (Scientific Name, Family and Scientific Family) should populate from custom fields in the related post – ID 65.

    Thanks for your help – much appreciated!

  • @trenbania Just to make sure, are the fields ‘scientific_name’, ‘family’ and ‘scientific_tree_family’ assigned to the post type you’re pulling in via the relationship field? And those fields are not in group field or repeater? You shouldn’t have to but may need to pass the post id when retrieving the field values.

    Maybe try:

    $posts = get_field('info_from_growing');
    if( $posts ) {
    foreach( $posts as $post) { // variable must be called $post (IMPORTANT)
    setup_postdata($post);
    $id = $post->ID;
    $scientificName = get_field('scientific_name', $id);
    $family = get_field('family', $id);
    $scientificTreeFamily = get_field('scientific_tree_family', $id);
    // do something with the values
    ?>
    <div>
        <p>Scientific Name: <?php echo $scientificName; ?></p>
        <p>Family: <?php echo $family; ?></p>
        <p>Tree: <?php echo $scientificTreeFamily; ?></p>
    </div>
    <?php
    }
    }
  • @kiwicreative well would you believe it. That works on the page template (yet to test the block).

    Yes, they’re simple text fields, but adding the post id pulled them in.

    I’ll add it to the block template and see if that works too.

    Thanks!

  • @kiwicreative I added the code to the block, but this is where I think something more fundamental is going on.

    The $posts variable seems to be empty. I have a block that adds custom fields to the page they’re assigned to. That works fine.

    What I want to do is pull the same fields into the related page in a block. The if( $posts ) conditional appears to evaluate as false and renders nothing in the block.

  • @trenbania So if I’m understanding you correctly you are trying to retrieve field values in a block…from a block on a different page? If that is the case then I have no idea how to do that.

    Normally when I use the relationship field its retrieving posts from a specific post type. On CPT’s I don’t use Gutenberg, I just create the acf fields that are assigned to the cpt, not a block. Then the code we were discussing would work. But my CPT’s are very standardized and don’t have the need for arbitrary content via blocks. I have never had to retrieve values in a block from another block on a different page.

    You may be able to use get_post_meta() but since I have never tried that with ACF I’m not sure exactly how that will work, or if it will work at all. Here is a forum topic I found real quick that might help you. https://support.advancedcustomfields.com/forums/topic/want-to-use-get_post_meta-instead-of-get_field-for-array/ You might need to also do a check to see if the block exists using has_block() https://developer.wordpress.org/reference/functions/has_block/

    Sorry couldn’t be more help man.

  • @kiwicreative – sorry for not thanking you sooner! I changed tack as I needed to get the project rolled out, but I will investigate soon enough to see if I can solve it. I have a feeling it should work and could be useful, but I had a fallback that did the trick.

    All the best
    Andy

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

The topic ‘Relationship field in a block’ is closed to new replies.