Multiple values in a ACF relational import field possible?

Closed

Import WP Addon - Advanced Custom Fields Updated 2 months ago 10 Replies

Geert asked 3 months ago on August 15, 2024 at 9:43 pm

Hi,

Previous you helped me with importing an ACF relational field value from Excel. But now I have the same, but multiple values in one cell which I divided by ;. So for one record to import I have multiple related fields that need input.

Is that possible?

Best, Geert

J

James replied Support Agent

3 months ago on August 16, 2024 at 6:42 pm

Hi Geert,

The previous method would have worked if you had seperated the values with a comma, currently there is know whay to choose which delimiter is used for the iwp_fn_get_posts_by function, so i have created a wrapper function that will change your ";" to ",".

function iwphd_custom_get_posts_by($input = '', $field = 'post_title', $post_type = 'post'){
$input = implode(',', explode(';', $input));
return iwp_fn_get_posts_by($input, $field, $post_type);
}

You would use this like the previous method:

[iwphd_custom_get_posts_by("{13}", "post_title", "bible-verse")]

James

GK

Geert replied

2 months ago on September 12, 2024 at 6:50 pm

Hi,

Thanks for the reply. This is nice. It took me a while but I don't get this one working. I made another import with the same complexity, but face the same problem. Maybe some parameters that are important that I have wrong.

- Do I need the field to put object or ID on output? For my code I use ID's (get related to show on pages)

- I guess I need to activate both CPT to be able to link, what I did - but does "search" matter on the field?

- I am linking a different field {34} to CPT "scripture" but not to "post_title" but to "verse_name_enu"

That gives me the shortcode:

[iwphd_custom_get_posts_by("{34}", "post_title", "scripture")]

(with correct preview returning)

And snippet:

function iwphd_custom_get_posts_by($input = '', $field = 'post_title', $post_type = 'scripture'){

    $input = implode(',', explode(';', $input));

    return iwp_fn_get_posts_by($input, $field, $post_type);

}

Thnx, Geert

 

J

James replied Support Agent

2 months ago on September 14, 2024 at 9:17 am

Are you able to export your acf field group, and attach a copy of the csv/xml file so that i can replicate your setup locally?

Thanks
James

GK

Geert replied

2 months ago on September 14, 2024 at 12:47 pm

I did some extra checks, since I was not sure the relational field was in both CPT's. Having a lot of fields makes it a bit harder to keep structure. I am trying to re-use fields on multiple CPT's to avoid creating many of the same.

Anyway, I found some relation added where it was a single verse. And while the relational field was NOT SET to bidirectional. When set to bidirectional (to self) it looks like it looses a link orso.

See attachments.

Just something else, do you do paid work too?

J

James replied Support Agent

2 months ago on September 17, 2024 at 7:03 pm

Thanks, Running this importer, the Related Entity Reference field is being populated on both sites for me.

You could try forcing the relationships for that field to update by adding the following custom code:

add_filter('iwp/acf/value', function ($value, $field) {

    if (isset($field['bidirectional']) && $field['bidirectional'] == 1) {
        add_filter('acfe/bidirectional/force_update/name=' . $field['name'], '__return_true');
    }

    return $value;
}, 10, 2);

James

GK

Geert replied

2 months ago on September 17, 2024 at 8:13 pm

Hi thanks for looking into it. Does that also work for multiple values on your side? I tried again, but single ones seem to work when the field is on object (not on id) but the multiple verses split on ; won't enter.

J

James replied Support Agent

2 months ago on September 17, 2024 at 8:22 pm

What do you mean by "when the field is on object (not on id)"?

GK

Geert replied

2 months ago on September 17, 2024 at 8:25 pm

The return format of the ACF field.

If you want, you can login.

J

James replied Support Agent

2 months ago on September 17, 2024 at 8:30 pm

If you update, the previous custom code to the following (it should work with multiple ids):

add_filter('iwp/acf/value', function ($value, $field) { if (isset($field['bidirectional']) && $field['bidirectional'] == 1) { add_filter('acfe/bidirectional/force_update/name=' . $field['name'], '__return_true'); $value = explode(',', $value); } return $value; }, 10, 2);
GK

Geert replied

2 months ago on September 17, 2024 at 8:47 pm

Looks like this works now, thanks! Will test further.