Last active
February 14, 2024 20:12
-
-
Save zackpyle/387bd602dfc8ad269af09f9c73442839 to your computer and use it in GitHub Desktop.
Set a default taxonomy #ACF value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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