Home › Forums › General Issues › Get all fields with a certain setting
I needed to show any custom field for a post that had a certain setting on it. This way if down the line the client adds more custom fields they want to be shown, no code would need to be added to show the new fields. They would show if they had the setting checked off.
I’m not sure there are easy functions for checking a setting on a field without getting all the settings.
Here is how I did it.
First I had to make the setting:
//add new field called special
function ign_admin_field_settings( $field ) {
acf_render_field_setting( $field, array(
'label' => __( 'Special' ),
'instructions' => 'If this field is a special field make sure to check this off so the field shows under the special section.',
'name' => 'special',
'type' => 'true_false',
'ui' => 1,
), true );
}
add_action( 'acf/render_field_settings', 'ign_admin_field_settings' );
Once this setting was in place, whenever I made a custom field I could check if it was “special”.
On the post in the template file, I then got all fields with the setting “special” like this:
$all_fields = get_fields(); //gets all fields
foreach ( $all_fields as $field => $values ) {
//get the field and all it's settings
$field_settings = get_field_object( $field, get_the_ID() );
if ( isset( $field_settings['special'] ) && $field_settings['special'] ) {
//do stuff with $field_settings. Output this special field.
}
}
Figured this might help someone else out there. It’s really useful
I do wish there was an easier way to get a fields setting without having to get everything else in the field.
This would be the only way to do it. Even if you wanted to get just one setting for a field ACF still needs to get the entire field definition. ACF needs to do this anyway for each field to know what type of field it is and other information so that it knows what to do with the value. This is why the dev gave us local JSON files for the group to cut down on DB queries. You getting the field object is not causing any extra work than is already being done.
If this is something you need to do a lot then you could create your own function
function my_acf_get_field_setting($field_name, $setting, $post_id=false) {
$value = NULL;
if (!$post_id) {
$post_id = get_the_ID();
}
$object = get_field_object($field_name, $post_id) {
if ($object && is_array($object) && isset($object[$setting])) {
$value = $object[$setting'];
}
return $value;
}
then in your template
$setting = my_acf_get_field_setting($field, 'special');
https://www.alledchemist.com/shop/buy-viagra-100mg-sildenafil-100mg/
https://www.alledchemist.com/shop/order-cialis-20mg-tadalafil-20mg/
https://www.alledchemist.com/shop/cenforce-100/
https://www.alledchemist.com/shop/cenforce-150/
https://www.alledchemist.com/shop/cenforce-200/
https://www.alledchemist.com/shop/buy-fildena-100/
https://www.alledchemist.com/shop/vidalista-60/
https://www.unitedpillshop.com/online-buy/buy-online-viagra-100mg-sildenafil-100mg/
https://www.unitedpillshop.com/online-buy/tadalafil-cialis-20mg-paypal/
https://www.unitedpillshop.com/online-buy/fildena-100mg/
https://www.unitedpillshop.com/online-buy/cenforce-100mg-paypal-usa/
https://www.unitedpillshop.com/online-buy/cenforce-150-mg-buy-online/
https://www.unitedpillshop.com/online-buy/buy-cenforce-200-mg-cheap-price/
The topic ‘Get all fields with a certain setting’ is closed to new replies.
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.