The value you would check for in your pre_save_post filter would be the same value you assign to “post_id” when setting up acf_form(). “new_post” is used as a default by ACF. If you use “new_post” in your acf_form() then ACF has a built in filter that runs on priority of 5 that will automatically create a new post of the post type “post”.
If you are creating a new “post” then in most cases there isn’t any reason to have a pre_save_post filter of your own.
What are you doing in your pre_save_post filter that requires special processing?
Group field sub fields can be accessed by shortcode using "{$group_field_name}_{sub_field_name}"
All keys are unique and stable once they are created. They should never change. The only thing that will remove a field key is deleting a field.
Field key => field name references for existing data can be broken by moving a field into or out of a field that can have sub fields, this includes group, repeater, flex and clone fields.
Field keys do not change based on post or anything else, they are not dynamic.
The only time the same field might have a different key is when using a clone field. Field keys for cloned fields are determined by the clone field settings. These field keys could be the same or they could be a concatenation of the parent field/child field keys. But even here the field key would be stable for that particular parent/child instance.
ALL functions is ACF for saving and getting fields are based on the field key, field names are provided for our benefit to make coding easier for 99% of users. ACF looks up the field key for a field name by getting the field key reference meta value from the database. This is not the same for blocks. I know that the ACF uses a similar mechanism for blocks but I do not know where or how the field key reference is located in this case. Blocks are actually outside of my experience.
Thank you, @hube2, this is super helpful and I really appreciate you taking the time to walk me through the internal data structures. I had gleaned a few of these points from your other posts in the forum, but this really pulls it all together.
One last question: are group/field keys stable (e.g., all references to a particular block type attribute/field are identical, regardless of post or block instance w/in a post) or are they dynamic per post/block?
I hope this is useful information for others in the future, and thank you for all you do!
When changing the ACF for this user object to return the user array, I get all the data for this “manager’. But I can’t parse out just the display_name. I’ve tried this code with no luck
$args = array('orderby' => 'display_name',);
$users = get_users($args);
foreach($users as $user){
$userID = 'user_'.$user->ID ;
echo '<li>';
echo '<b style="font-size:14px;">' . $user->data->display_name . ' x' . $user->emp_office_ext . '</b><br/>';
echo '<em style="font-size:12px;white-space: nowrap; ">' . get_field('position',$userID) . '</em>';
if (!empty(the_field('manager',$userID))){
$manager = get_field('manager',$userID);
echo $manager->display_name;
}
echo '</li>';
}
Where are you trying to use the_permalink()?
That aside, you have a query nesting issue.
Please see this topic and the topics that are linked to from there for an explanation of nested query issues. https://support.advancedcustomfields.com/forums/topic/nested-post-object-fields/
“As long as a field name is not duplicated there isn’t an issue.”
Sorry, For clarification over the use of Field Name, I’m not talking about the label.
Example…
Field Group One would contain:
Field Label “Wattage”, Field Name “wattage”, Field Key “field_01xtine3m”
Field Label “Max DB”, Field Name “max_db”, Field Key “field_z9tebe9n5”
Field Label “Frequency”, Field Name “frequency”, Field Key “field_59ddt5qec”
Field Label “Avg Temp”, Field Name “avg_temp”, Field Key “field_n5bew9te9”
Field Label “Impedance”, Field Name “impedance”, Field Key “field_nti5bxte7”
Location Product Type is equal to “b”
Assigned to
Field Group Two would contain:
Field Label “Wattage”, Field Name “wattage”, Field Key “field_tit501xqec”
Field Label “Max DB”, Field Name “max_db”, Field Key “field_ebz9tne3m”
Field Label “Frequency”, Field Name “frequency”, Field Key “field_de59d9n5”
Field Label “Pressure”, Field Name “pressure”, Field Key “field_te57t501x”
Field Label “Inductance”, Field Name “inductance”, Field Key “field_te9915s0”
Location Product Type is equal to “c” or Product Type is equal to “d”
Field Group Three would contain:
Field Label “Wattage”, Field Name “wattage”, Field Key “field_ti9d9n5tec”
Field Label “Max DB”, Field Name “max_db”, Field Key “field_eb501z9xq”
Field Label “Frequency”, Field Name “frequency”, Field Key “field_dee3m5tn”
Field Label “Strand Count”, Field Name “strand_ct”, Field Key “field_dfa36xtn”
Field Label “Channels”, Field Name “channels”, Field Key “field_dtn6ga3x”
Location Product Type is equal to “e”
Would this work fine or lead to issues down the road?
I can’t give you any specific advice.
Getting the field keys is not going to be an easy task.
ACF field groups are posts of the type “acf-field-group” the “post_name” value is the group key. “post_content” holds the settings of the field group.
ACF fields are posts of the type “acf-field”. These are child posts of either the group post or another field post for sub fields. The “post_name” is the field key. “post_content” holds most of the field settings. Some settings are in the _postmeta for the field post.
Anyway, that’s how ACF stores information.
Getting that information.
There is an internal, undocumented function in ACF: acf_get_field_groups() and you will find it in /includes/acf-field-group-functions.php. This function takes one argument that is an array of “filters”. These filters will get the field groups according to the location rules. For example a filter value of array('post_id' => $post_id)
would get all of the field groups that would appear when editing a specific post ID. I do not know all of the possible settings for this but you should be able to get field groups with location rules for a specific “block” and you’re going to have to dig through the ACF code to determine what to use for the filters to do this.
Once you do the above then there is another undocumented function acf_get_fields(). This in in the file /includes/acf-field-functions.php. This takes one argument $parent. This can be the field group key. This will get a nested array of fields that will look like the array for the field group that you’d get if you exported the field group to PHP.
The above is the process that ACF goes through when showing the field groups in the WP admin.
To get the field key of a field you would then have to loop over the fields, and sub fields if you have them (recursive function) to get the field information. Here you would still need something to search for to find the field in question, for example the field name, in order to get the field key. In the end something would still need to be hard coded, in this case the field name and or parent field name if looking for a sub field. In the case of sub fields you would also need to branch your recursive function depending on why type of parent field you are looking at.
Doesn’t seem like anything I’ve read will give me the desired result.
Circling back to the original question: Can fields sharing the same name be filtered and collected together despite having different field keys. If so are there any long-term issues when using fields of the same name for the same purpose in several different field groups?
The most likely reason for this is a pre_get_posts action that is interfering with the ACF query that is run when getting the image post. The usual case for this is not checking that the pre_get_posts filter is acting on the “Main Query” [is_main_query()]. There could be other issues, but 90% of the time this is the cause.
Hello again,
Worked all night on it and did a lot of searching. It seems it was due to a post-reordering plugin that conflicts with tax_query()
So glad I found the issue 😉
There seems to be an issue with updating sub fields of a group field. It is treated as a repeater, as was discussed in the other topic.
A group field is actually a repeater that always has one row. update_sub_field() is looking for a row index in the second array index array($repeater_key, $row_index, $sub_field_key)
. There is no row index for a group field and if you supply a row index ACF will save the value using the incorrect meta key of "{$repeater_name}_{$index}_{$sub_field_name}"
In the other thread the person was updating the sub field inside a have_rows() loop and ACF was assuming the index of 0
, which would be correct for the first row of a repeater.
There is likely a bug, I can’t fix bugs, this would have to be reported to the developers. I don’t use update_sub_field() as a general rule so it is only recently, that other topic, that brought it to my attention. My opinion when updating repeaters, or group fields, is to use a similar method to this, updating the entire repeater rather than a sub field. On the other hand, I almost never update fields from code.
Yes, you’ll need to add some custom conditional logic, I don’t have an answer for you but you will find some solutions to start from in [FORUM SEARCH RESULTS]
Hi John,
This is in a JS file thats being enqueued in the themes functions.php
It’s probably easier to tell you what i’m trying to achieve.
I have a page that loads a custom post type called projects in a isotope.js grid.
I’ve got some filtering going on using a URL hash. EG. /projects/#tag=green or /projects/#network=bbc
that’s all handled in javascript.
When a tag is loaded i want to be able to pull a title & an acf field from the taxonomy term of the tag “green” and update a classes innerhtml with the loaded data.
As far as i’m aware php can’t access the hash and java is the way to do it.
Noting that the WP block-types
endpoint would seem to be a good place for this data to live, but no detail is included by default in the attributes
field. There’s a data
item but it just specifies:
"data": {"type": "object", "default": []},
Did you read the solution I gave in the other topic?
Here it is adapted for your field keys
// initialize row value just in case the loop returns noting
$row = array();
// loop group field and get existing values
if (have_rows('field_64b38c4e3ec56', $post_id)) {
// this is always true once
while (have_rows('field_64b38c4e3ec56', $post_id)) {
// this loop always happens 1 time for a group field
// *********************************************************
// little known fact
// the_row() returns an array holding the row
// with field keys as indexes and unformatted field values
// *********************************************************
$row = the_row();
}
}
// set a new value for the "sub field" that needs to be updated.
$row['field_64c782fc4c55d'] = 'value you want to update to here';
// update the group field
update_field('field_64b38c4e3ec56', $row, $post_id);
Thanks for your response, John.
I had no idea that ACF supported the use of shortcodes!
Using this newly-gained knowledge from you, I understand that because I am getting the ACF fields from anywhere other than the current post (for me it would be one of many pages), then using a shortcode in the format [acf field="field_name" post_id="123"] should get me what I need, substituting the word ‘options’ rather than a post ID.
Because one of the field names in what I created was business_name, I’ve created this shortcode: [acf field="business_name" post_id="options"] and placed it within some plain text.
Using this within an Elementor text editor field doesn’t yield any result. Literally nothing appears on the front end other than the plain text preceding and after those square brackets.
What am I doing wrong?
No, there is no solution. The only integration with elementor comes from elementor. ACF has no built in integration for page builders.
Elementor is what is striping the script tags, not ACF. ACF will output whatever is in a text area without stripping tags as long as it is set to not format the content in the return value settings.
This is why you need to use a shortcode. If the content being output is a field on the current post then you can use [acf field="field_name"]
. Others have used this method because elementor does not appear to affect content returned by a shortcode.
https://www.advancedcustomfields.com/resources/shortcode/
You stated, “It is not that confusing, if it is a sub field then you are using the wrong function/method to update the value.” but it is. See below:
Name: FRESH SNOW SKI RESORTS”
Key: group_647d9f906d00a
In the above group I have FIELDS:
Name: FS SKI RESORT DATA (and it is defined as a group
Key: field_64b38c4e3ec56
In above, I have many sub fields and here is an example of one:
Field Name: qgis_aa_zoom
Key: field_64c782fc4c55d
Numeric
I assume field_64c782fc4c55d (qgis_aa_zoom) is a sub field, right?
Why does this work (although it does not show in the view as mentioned previously):
$result = update_field(field_64c782fc4c55d,11, 1110);
$arraydata = get_field_object(field_64c782fc4c55d,1110);
echo $arraydata['value'];
and this not work;
$result = update_sub_field(array('field_64b38c4e3ec56', 'field_64c782fc4c55d'), 3, 1110);
$arraydata = get_field_object(field_64c782fc4c55d,1110);
echo $arraydata['value'];
Is there any other way to post my php with out giving it to logged in users?
Without more context these questions may make sense to you but they make little sense others.
1) What type of field? What type of user? Where will the field be displayed/used?
2) What do you mean by “group edit”? this has not meaning to me.
Error: Duplicate reply detected; it looks as though you’ve already said that.
It is so very bizarre and do not understand what is going on. I am hoping you can answer some of my previous questions. It would help me understand what’s going on.
As already explained under FIELD GROUPS I have:
Name: FRESH SNOW SKI RESORTS”
Key: group_647d9f906d00a
In the above group I have FIELDS:
Name: FS SKI RESORT DATA (and it is defined as a group
Key: field_64b38c4e3ec56
In above, I have many sub fields and here is an example of one:
Field Name: qgis_aa_zoom
Key: field_64c782fc4c55d
Numeric
Now, if I do this;
$result = update_field(field_64c782fc4c55d,11, 1110);
$arraydata = get_field_object(field_64c782fc4c55d,1110);
echo $arraydata['value'];
I get 11. If I change to 7, I get 7. It seems to update. Is this because I am creating a new entry as a parent? Why would this work? When I look at my sub field view, it is not updated.
Now, if I do this:
$result = update_sub_field(array('field_64b38c4e3ec56', 'field_64c782fc4c55d'), 3, 1110);
$arraydata = get_field_object(field_64c782fc4c55d,1110);
echo $arraydata['value'];
It is not updated and gives me the old value from above on the screen.
No need to include a screenshot. My questions are simple.
1- How to make a field created for users without editing by the access level of the user.
2- How to group edit a selected field created for users.
No, I have stated previously that field_64c782fc4c55d is in the group field_64b38c4e3ec56. For field_64c782fc4c55d it says at the top of the group, “sub fields” and field_64c782fc4c55d is under that.
When I stated acf-field_64b38c4e3ec56-field_64c782fc4c55d, it is from the view html. Here it is again.
<input type="number" id="acf-field_64b38c4e3ec56-field_64c782fc4c55d" name="acf[field_64b38c4e3ec56][field_64c782fc4c55d]" step="any">
Thank you for your response, but the problem does not come from the small css or js files since these files are minified and compiled and each page has its own css and js without loading the blocks not present.
If I summarize all the sql requests made by acf for a page in terms of performance, here is the result with the Blackfire tool:
acf_block_render_template = 18.4%
acf_render_block = 19.14%
acf_meybe_get_field = 9.60%
acf_format_value = 10.11%
It shows that ACF consumes more than 57% of the requests just for the rendering of the blocks. So the impact is still huge.
Example of result for acf_meybe_get_field :
Yes, I have submitted a ticket. They acknowledge that there is a bug:
Unfortunately the issue with the preview appears to be related to a known bug with ACF and the WordPress block editor. The same issue can happen when previewing fields in an already published post. This is something that we’re planning to fix up in a future release.
In either case, the custom fields should show up in the frontend once the post has been published.
But they don’t think this could be the cause of custom field data not showing up on the frontend after scheduled posts are published.
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.