Home›Support›English Support›[Closed] switch from outputting ACF custom fields manually specifying the field to be displayed to getting all custom fields associated with the post
[Closed] switch from outputting ACF custom fields manually specifying the field to be displayed to getting all custom fields associated with the post
This is the technical support forum for WPML - the multilingual WordPress plugin.
Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.
I have been redirected here from Advanced Custom Fields support.
This is part of my message history with ACF:
I want to switch from outputting ACF custom fields manually specifying the field to be displayed to getting all custom fields associated with the post and looping through them. I am using get_field_objects() to accomplish this. The problem I am having is that fields that had been previously saved to the post but are not shown in the backend anymore are also being output. This is a problem because fields saved erronously are also being output. What would make sense to me is to output only fields that are actually visible in the backend as well. Which is defined by the location rules of the fields. In lieu of this, I have to delete the unwanted data manually one by one from the database, when and if they are discovered. I have a custom post type called machines that has a machine_type taxonomy with many terms. Based on these terms and other rules, certain fields are shown or hidden. Is there a way to do this?
################################################################################################
Hi Krisztian,
Thanks for your patience on this, I trust you're keeping safe!
Could you kindly try the code below and check if it can be of any help?
The setup can also be extended to include field value in the field by using the get_field() function as illustrated at hidden link.
Below is a sample code used in the screencast that you can modify to fit your use case.
$post_id ) );// retrieve groups associated with post.
foreach ( $groups as &$group ) {
$fields = acf_get_fields( $group['key'] );// retrieve fields associated with group.
foreach ( $fields as &$field ) {
$field['value'] = get_field( $field['key'], $post_id );// retrieve field value.
}
$group['fields'] = $fields;
}
return $groups;
}
?>
Custom soln
Please try this and let me know how it goes.
P.S. Please note that we do not offer coding assistance to our customers.
Kind Regards,
Chris
################################################################################################
Hi, thank you for this. It is very useful.
The problem I am facing right now is that if I am on a translated post, and I want to display source language (English) fields, this piece of code only gives me translated groups / field_objects. It doesn't matter if I supply an English post id through apply_filters('wpml_object_id', $ID, $post_type, false, 'en') or a translated one. Our translated posts mostly (not 100% though) use the same exact data as the English, so these fields are not translated at all. This function gives me only the fields of the translated post, and a lot of fields are therefore missing which should be coming from the English. I don't know if this has anything to do with automatic id adjustment or not, but if yes, I would need to be able to block it for this function only, so I do get the English field objects.
I experimented with get_field_objects with the english ID and this works, but again, then the problem is the one I started with.
After setting up the sandbox to duplicate the problem on my site. I am not getting the error. So I would need some pointers as to what could possibly cause this, so I have an idea where to look.
Additional information: So on my staging site, I found out that the function works with the post post type(after setting up new custom fields to test the functionality). But it doesn't seem to work with my custom post type.
So, If I understand the issue correctly the exact same code and post type setup did not created an issue on the Sandbox test site. However it causes issue in your staging site, did I got it correct?
If yes, this could mean that some other plugin/code or theme in your staging site is interfering to cause the issue. In that case please run the following test and see how it goes:
- Backup the site first to prevent any data loss.
- Deactivate all plugins except critically required for the site Like WPML, ACF and your custom code. Also change the theme to a default one ( twenty twenty one ) and if required copy the code for the custom field to the default theme.
- See if the issue is there or not, if not, activate original plugins and theme one at a time each time testing the issue to see which one starts the issue.
I used a simple post setup at the beginning. It seemed to work with that.
Later I then tried a custom post type, and some of my custom fields which I imported from my live site. This didn't seem to work. I got a bit lost on setting up how to display the custom post type field contents, and then I got logged out and had no way of logging back in.
The topic ‘[Closed] switch from outputting ACF custom fields manually specifying the field to be displayed to getting all custom fields associated with the post’ is closed to new replies.