Home › Forums › Add-ons › Repeater Field › Save ACF relationship (name) value to ACF text field
I try to achieve the following. I have two post types, “projects” & “kuenstlerinnen” (artists) Within the post type “projects”, there’s an ACF relationship field setup, connecting these two to together → “project_to_kunstlerin”
The post-type “künstlerinnen” (Artists) contains two ACF text fields: first-name (“artist_vorname”) & Last- or Group-Name (“artist_nachname”). There is a code snippet running, which saves both fields to the title → “artist_vorname””artist_nachname”
I know would like to have a hidden text field within the projects post type, which is automatically populated with the first value of the “project_to_kunstlerin” field. It should only use the last name field. The reason I try to get this to work, is that I can order by this hidden field, so I get the posts ordered by the last name of the artist.
So far I come up with the following code-snippet:
<?php
//Make special artist field autosave for sorting
function write_artist_name_to_hidden( $post_id ) {
$post_type = 'projects'; //custom post type for events
//Check if we are saving correct post type
if( get_post_type( $post_id ) != $post_type)
return;
//Check it's not an auto save routine
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
// get the artist id of the post. Note this is in a relationship field
$nameofartist = get_post_field ('project_to_kunstlerin', $post);
// update the artist value
update_field( 'project_kunstlerin_nach_name_sort', $nameofartist, $post_id );
}
add_action('save_post', 'write_artist_name_to_hidden');
In the backend, it now populates the destination ACF field with:
One artist: “[“37″]” Two artists: [“37″,”38”]
These are the IDs.
How can I get another value there instead of the post id?
In theory, it must be something like:
Check with IDs are in the “project_to_kunstlerin” relationship field.
Take the first ID.
Look up, which value is stored in “artist_nachname” of ID in post type “kuenstlerinnen”
Maybe anybody of you has an idea how to get this to work? Your help is highly appreciated!
1) change your filter to an acf/save_post filter with priority > 10 ensure that values are saved to the DB before you try using them.
2) Get the value of the relationhip field, unformatted, from the project post being saved.
$kuenstlerinnen_id = get_field ('project_to_kunstlerin', $post_id, false);
The above value could be an array
if (is_array(kuenstlerinnen_id)) {
$kuenstlerinnen_id = kuenstlerinnen_id[0];
}
get the last name field from the other post
$artist_nachname = get_field('artist_nachname', $kuenstlerinnen_id;
update the field on the project with this value
update_field('project_kunstlerin_nach_name_sort', $artist_nachname, $post_id);
Hello John!
Thanks very much for your help!
I have tried to set up a function with your changes as follows:
<?php
//Make special artist field autosave for sorting
function write_artist_name_to_hidden( $post_id ) {
//Get the value of the relationhip field, unformatted, from the project post being saved.
$kuenstlerinnen_id = get_field ('project_to_kunstlerin', $post_id, false);
//The above value could be an array
if (is_array('kuenstlerinnen_id')) {
$kuenstlerinnen_id = kuenstlerinnen_id[0];
}
//get the last name field from the other post
$artist_nachname = get_field('artist_nachname', $kuenstlerinnen_id);
//update the field on the project with this value
update_field('project_kunstlerin_nach_name_sort', $artist_nachname, $post_id);
}
add_action('acf/save_post', 'write_artist_name_to_hidden', 11);
Sadly I am doing something wrong here, as get several warnings when I try to update a post:
Warning: Illegal offset type in isset or empty in /Users/sebastianalbert/Local Sites/fotohof-calling/app/public/wp-content/plugins/advanced-custom-fields-pro/includes/local-meta.php on line 195
Warning: Illegal offset type in isset or empty in /Users/sebastianalbert/Local Sites/fotohof-calling/app/public/wp-content/plugins/acf-extended/includes/core/meta.php on line 168
Warning: Illegal offset type in isset or empty in /Users//Local Sites/fotohof-calling/app/public/wp-content/plugins/advanced-custom-fields-pro/includes/local-meta.php on line 195
Warning: Illegal offset type in isset or empty in /Users//Local Sites/fotohof-calling/app/public/wp-content/plugins/acf-extended/includes/core/meta.php on line 168
Warning: Cannot modify header information – headers already sent by (output started at /Users//Local Sites/fotohof-calling/app/public/wp-content/plugins/advanced-custom-fields-pro/includes/local-meta.php:195) in /Users//Local Sites/fotohof-calling/app/public/wp-includes/pluggable.php on line 1340
Warning: Cannot modify header information – headers already sent by (output started at /Users//Local Sites/fotohof-calling/app/public/wp-content/plugins/advanced-custom-fields-pro/includes/local-meta.php:195) in /Users//Local Sites/fotohof-calling/app/public/wp-includes/pluggable.php on line 1343
Do you have any idea what I am doing wrong here? I am pretty new to the php stuff.
Thanks again for your help!
There is nothing in the code posted that should cause the errors your are getting.
That is strange.
I am using advanced scripts for my code snippets. As soon as I activate the script above, the error is there (when I save a post). When I deactivate the script, the error is gone.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.