Site logo

[Resolved] Element Content Template and ACF Relationship field

Home Forums Support [Resolved] Element Content Template and ACF Relationship field

Home Forums Support Element Content Template and ACF Relationship field

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #2543754
    Simon

    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

    #2544286
    David
    Staff
    Customer Support

    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….

    #2552759
    Simon

    Hello,

    First, I apologize for my late return.
    I had to step away from the screen for several days.

    I have tried to apply your method but so far without success.

    I have attached the captures of my configuration:
    https://drive.google.com/drive/folders/1rE0kawuUBTkAXqZyodrLfPcWs6xpU3Kf?usp=sharing

    Thanks

    #2553061
    David
    Staff
    Customer Support

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

    #2553096
    Simon

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

    #2553735
    David
    Staff
    Customer Support

    I made an edit to the PHP Snippet above

    Can you try that ?

    #2553821
    Simon

    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.

    #2553822
    Simon

    Solved

    #2553986
    David
    Staff
    Customer Support

    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.

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.