Home Forums General Issues What is stored in the Relationship Field?


What is stored in the Relationship Field?

  • I know that you can choose Post Object, or Post ID, so specifically this means that it will be either an array or ID? So if I wanted to put a specific ID into the field would I be able to create a variable and place that ID into the field?

    So as an example…

    function store_relationship_id( $args ) {
    	echo '<input type="hidden" name="relationship_field" value="MyValue"';
    add_action( 'acf/input/form_data', 'store_relationship_id', 10, 1 );

    Will this get stored correctly?

  • The Relationship field holds an array of post IDs. I don’t think you can add a hidden field and do this, ACF uses the field IDs in the form and the posted data all in the “acf” index of $_POST.

    I think you need to create an acf/save post filter to add posts to the relationship field.

  • So if I’m understanding this hook correctly I’d be able to do something like this:

    function my_acf_save_post( $project_id ) {
        // bail early if no ACF data
        if( empty($_POST['acf']) ) {
       //obtain the current page's post ID
    	$project_id = get_the_ID();
        // array of field values
        $fields = $_POST['acf'];
        // specific field value
        $field = $_POST['acf']['assigned_project'];
    // run before ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 1);
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    And $project_id would be stored in the ‘assigned_project’ acf field?

  • Thanks, after looking over a few more documents and other examples I was able to figure it out! Really great plugin, makes application creation very easy for me.

  • Sorry I didn’t get back about to you on your follow up, glad you got it worked out.

    That’s what I use ACF for, application development. WP is a good base to start with and ACF really helps with making it into a real CMS.

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

The topic ‘What is stored in the Relationship Field?’ is closed to new replies.