Site logo

Archived topic

Element Content Template and ACF Relationship field

8 replies · Started by Simon on February 23, 2023

Viewing posts 1–9 of 9

Hello,

I am looking to link 2 Custom Post Types: Case Studies and Testimonials.
At the bottom of each Case Study, I would like to post a related testimonial.

I use ACF Pro, GeneratePress Premium and GenerateBlocks Pro.

I created ACF fields for Case Studies and for testimonials.

For the Testimonials content type there are 3 fields: a field for the testimonial text, a field for the name and a field for the function.

Among the Case Studies fields, I created a Relationship ACF Field that filters by the Testimonials content type and returns the Post ID.
When I edit a Case Study, I can choose the testimonial associated with it.

The presentation of each Case Study is managed by a GeneratePress Element of type Block Content Template.

My question: How can I display the ACF fields (text, name, function) of the associated testimonial in the Case Study Content Template?

Thank you

Hi there,

hmmm... so GenerateBlocks Dynamic Data doesn't support all Pro ACF options yet, such as Relationships.
What you could try is:

1. Use a Query Loop block. Set its Params for post type and number of posts, and nothing else.

2. Use a filter to merge its query args to include your Relationship post ID:

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    // find the block with <code>my-class-name</code>
    if (
        ! empty( $attributes['className'] ) &&
        strpos( $attributes['className'], 'my-class-name' ) !== false &&
        ! is_admin()
    ) {
        // get the relate post ID
        $queried_object = get_queried_object () ;
        $relationship = get_field( "related", $queried_object );
        // Merge the current $query_args with the new args
        return array_merge( $query_args, array(
            'post__in' => $relationship,
        ) );
    }

    return $query_args;
}, 10, 2 );

NOTE: that you set your my-class-name and that needs to be added to the Query Loop -> Grid Block --> Advanced > Additional CSS Classe
And update the get_field value for your ACF Field.

3. Then in that Query Loop you should be able to set the Dynamic data you need on the relevant GB blocks..

But i have not tested this....

What happens if you change the custom fields Return Type to Post ID ?

No sorry, nothing is returned (nothing is displayed).

I made an edit to the PHP Snippet above

Can you try that ?

Helo,

Great, it works.

Maybe the first version of your code worked.
I actually found that in my Query Loop I should not choose the content type that contains the repeater field (Case Studies) but the content type that is referred to in the repeater (Testimony).
The parameters that are shown in capture 03-query-loop.png are apparently not good.

Anyway it works with the latest version of the code and the repeater field set to Return Format: Post ID.

I take this opportunity to ask you if you have any hints about the release of a new version of GenerateBlocks compatible with the advanced fields of ACF (Repeater, Relationship, Flexible Content…).

Thanks again for your help.

Solved

My first code was using the wrong parameter, cause i was being stupid lol

We're currently working on Dynamic Data options to give them way more flexibility then they have.
Once thats done we will be revising the Query Loop so it can handle other content types, not just posts.

So its not going to be real soon, but its high on our list for this year. I hope sooner rather than later.

This archived topic is closed to new replies.