Skip to content

Instantly share code, notes, and snippets.

@zackpyle
Last active March 8, 2024 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zackpyle/79b095a9f36640e432b3d5d29b561770 to your computer and use it in GitHub Desktop.
Save zackpyle/79b095a9f36640e432b3d5d29b561770 to your computer and use it in GitHub Desktop.
Set a different default #ACF value based on the post type
<?php
add_filter('acf/load_field/key=YOUR_FIELD_KEY_HERE', function ($field) {
if (get_post_type() == 'post') {
$field['default_value'] = 'YOUR_DEFAULT_VALUE_HERE';
}
return $field;
});
// If the field type is a checkbox, then give it an array (even if it is a single value)
add_filter('acf/load_field/key=YOUR_FIELD_KEY_HERE', function ($field) {
if (get_post_type() == 'post') {
$field['default_value'] = ['choice1', 'choice3'];
}
return $field;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment