Skip to content

Instantly share code, notes, and snippets.

@zackpyle
Last active February 14, 2024 20:12
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/387bd602dfc8ad269af09f9c73442839 to your computer and use it in GitHub Desktop.
Save zackpyle/387bd602dfc8ad269af09f9c73442839 to your computer and use it in GitHub Desktop.
Set a default taxonomy #ACF value
<?php // ignore - for gist formatting
// Specify your ACF field here
add_filter('acf/load_field/name=your_field_name', function ($field) {
global $pagenow;
// Set the default term ID
$default_value = 'XXX';
// Apply the default value only when creating a new post
if ($pagenow == 'post-new.php') {
$field['value'] = $default_value;
}
return $field;
});
<?php // ignore - for gist formatting
// Specify your ACF field here
add_filter('acf/load_field/name=your_field_name', function ($field) {
global $pagenow;
// Set the default term ID
$default_value = 'XXX';
$target_post_type = 'your_post_type'; // Specify the target post type
// Apply the default value only when creating a new post and only for the target CPT
if ($pagenow == 'post-new.php') {
if ((isset($_GET['post_type']) && $_GET['post_type'] === $target_post_type) ||
(!isset($_GET['post_type']) && $target_post_type === 'post')) {
$field['value'] = $default_value;
}
}
return $field;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment