Populate ACF Relationships

Populate each related post as a separate choice when populating data from an ACF Relationship custom field.

Read the Walkthrough

Code

Filename: gppa-populate-acf-relationships.php

<?php
/**
 * Gravity Perks // Populate Anything // Populate ACF Relationships
 *
 * Populate each related post as a separate choice when populating data from an ACF Relationship custom field.
 *
 * Plugin Name:  GPPA ACF Relationships
 * Plugin URI:   http:///gravitywiz.com.com/documentation/gravity-forms-populate-anything/
 * Description:  Populate each related post as a separate choice when populating data from an ACF Relationship custom field.
 * Author:       Gravity Wiz
 * Version:      0.2
 * Author URI:   https://gravitywiz.com
 */
add_filter( 'gppa_input_choices', function( $choices, $field ) {

	if ( strpos( $field->cssClass, 'gppa-acf-relationships' ) === false ) {
		return $choices;
	}

	if ( rgars( $choices, '0/gppaErrorChoice' ) ) {
		return $choices;
	}

	$new_choices = array();

	foreach ( $choices as $choice ) {
		$post_ids = array_filter( explode( ',', $choice['value'] ) );

		foreach ( $post_ids as $post_id ) {
			$new_choices[] = array(
				'value' => $post_id,
				'text'  => get_the_title( $post_id ),
			);
		}
	}

	return $new_choices;
}, 10, 2 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.