Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

ACF Relationship Field and GenerateBlocks

  • Is there any support for ACF Relationship fields in GB? Specifically:

    1. I have a relationship field on Posts which creates a relationship with posts in a ‘members’ CPT. I want to set up a reverse query using the Query Loop Block (used on the ‘single’ Members CPT template) to retrieve blog Posts for which the current Members CPT post is set as a relationship. Along the lines of:

    
    $current_member_organisation_id = get_the_ID();
    $args = array(
    	'post_type' => 'post',
    	'meta_query' => array(
    		array(
    			'key' => 'member', // The ACF relationship field key
    			'value' => '"' . $current_member_organisation_id . '"',
    			'compare' => 'LIKE'
    		)
    	)
    );

    2. Elsewhere, I’d like to pull in the currently post’s relationship fields via Dynamic Data on the Headline block.

  • Hi there,

    theres no direct integration as yet.
    But you can write custom queries for a the Query Loop block.
    See here for a similar example:

    https://generate.support/topic/how-to-create-a-query-loop-for-custom-pages/#post-108624

  • David, you are, as ever, a flipping legend!

    For anyone else finding this post, this is my filter for this use case:

    //Custom query for GB Loop Block: 
    add_filter( 'generateblocks_query_loop_args', function ($query_args, $attributes) {
    	if (
    		! empty( $attributes['className'] ) &&
    		strpos( $attributes['className'], 'member-related-posts' ) !== false &&
    		! is_admin()
    	) {
    		// get the related post ID
    		$current_member_organisation_id = get_the_ID();
    		// Merge the current $query_args with the new args
    		return array_merge( $query_args, array(
    
    			'meta_query' => array(
    				array(
    					'key' => 'member', // The ACF relationship field key
    					'value' => '"' . $current_member_organisation_id . '"', // The current member organisation ID wrapped in quotes
    					'compare' => 'LIKE'
    				)
    			)
    		) );
    	}
    
    	return $query_args;
    }, 10, 2 );
  • Awesome – glad to be of help.
    I just spotted question 2 – do you need assist with that ?

  • If you have a solution for 2, that would be awesome… I’ve tried referring to the meta key but that doesn’t seem to work.

  • Option A – Query loop
    if all the related field data was grouped together then you could use a Query Loop block to return all the necessary fields in one go using the same method as above

    Option B – Shortcode.
    You could build a custom shortcode that allows you to specify the Relationship field and the Field value you want to return

    eg.

    
    function acf_relationship_shortcode($atts) {
        $atts = shortcode_atts(
            array(
                'field_name' => '',
                'related_field_name' => '',
                'post_id' => get_the_ID(),
            ),
            $atts,
            'acf_relationship'
        );
    
        $relationship_field = get_field($atts['field_name'], $atts['post_id']);
    
        if ($relationship_field) {
            $output = '';
    
            foreach ($relationship_field as $related_post_id) {
                
                $related_field_value = get_field($atts['related_field_name'], $related_post_id);
    
                $output .= $related_field_value . ' ';
            }
    
            return $output;
        }
    
        return '';
    }
    add_shortcode('acf_relationship', 'acf_relationship_shortcode');
    

    Then you could use the shortcode inside a headline block eg.

    
    [acf_relationship field_name="your_relationship_field_name" related_field_name="your_related_field_name"]
    

    Can’t say I am 100% confident on that as I don’t do much with ACF relationships but it kinda makes sense 🙂

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