Home Forums ACF PRO ACF Relationship Block Issue


ACF Relationship Block Issue

  • I have a range of Custom Post Types (programmes), with custom taxonomies.
    We need to display certain ‘programmes’ within certain pages.
    Using ACF Blocks (Relationship field) to select which programmes to display on each page.
    But what I am getting is the first selected programme is being replaced by the title, content & featured image from the page they are being shown on.
    Here’s the block code:
    I have also tried ‘Post Object’ field type – getting the same results.
    Can anyone help what I’ve done wrong in my code?
    I tried a wp_reset_postdata(); at the start of the block, it doesn’t clear the error.

    Here’s a screenshot to better explain:

  • If you configure the Post Object field to return a post object instead of the post ids, it will deliver an array of post objects ready to use. No need to call global $post, setup_postdata( $post ), or wp_reset_postdata(). All of that seems to just unnecessarily confuse the page that is loading the block. In order to retrieve the permalink, the title, the featured image etc. of the posts assigned to the block, just use $post->ID within the foreach ( $posts as $post ).

  • Thanks @daniel-buth That’s how I had the block setup originally.
    In ACF, it’s set to Post Object already.
    I have created a gist of the amended code:

    Now what I am getting is ALL the posts are displaying the content of the page they are being shown on. And not bringing in any of the selected items from the ACF Relationship field block.

  • You have it the other way around: You have to apply $post->ID to the functions in the template, not the foreach.
    The “within-the-loop”-functions, so to speak, such as get_permalink() use the current post to fetch information. That is how it is intended. But you can pass $post->ID as an argument to most of them (some have to be replaced), so they will fetch information on the current post of the foreach.
    Try this:

     * Programmes Block Template.
     * @param   array $block The block settings and attributes.
     * @param   string $content The block inner HTML (empty).
     * @param   bool $is_preview True during AJAX preview.
     * @param   (int|string) $post_id The post ID this block is saved to.
    // Create id attribute allowing for custom "anchor" value.
    $id = 'prog-' . $block['id'];
    if ( ! empty( $block['anchor'] ) ) {
    	$id = $block['anchor'];
    // Create class attribute allowing for custom "class_name" and "align" values.
    $class_name = 'progBlock';
    if ( ! empty( $block['class_name'] ) ) {
    	$class_name .= ' ' . $block['class_name'];
    if ( ! empty( $block['align'] ) ) {
    	$class_name .= ' align' . $block['align'];
    if ( $is_preview ) {
    	$class_name .= ' is-admin';
    <div id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class_name ); ?>">
    	$posts = get_field( 'progs' );
    	if ( $posts ) :
    	<div class="progGrid grid-x grid-margin-x grid-margin-y">
    		<?php foreach ( $posts as $post ) : ?>
    			<div class="card cell small-12 medium-6 large-4">
    				<img role="img" src="<?php echo get_the_post_thumbnail_url( $post , 'foureighty' ); ?>" alt="<?php echo get_the_title( $post ); ?>"/>
    				<div class="progText">
    					<div class="progTitle">
    						<h3 class="blog-title">
    							<a href="<?php echo esc_html( get_permalink( $post ) ); ?>" alt="<?php the_title_attribute( [ 'post' => $post ] ); ?>">
    								<?php echo get_the_title( $post ); ?>
    					<div class="progExcerpt">
    						<h3 class="blog-title show-for-large">
    							<a href="<?php echo esc_html( get_permalink( $post ) ); ?>" alt="<?php the_title_attribute( [ 'post' => $post ] ); ?>">
    								<?php echo get_the_title( $post ); ?>
    							<?php echo get_the_excerpt( $post ); ?>
    		<?php endforeach; ?>
    	<?php endif; ?>
  • Thanks @daniel-buth That’s where I had went wrong…
    Not seeing the bark on the trees.

    Thanks Man, appreciate it.
    Was pulling the hair out for a while there.

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

The topic ‘ACF Relationship Block Issue’ is closed to new replies.