I have a custom post type "Shipments" for a freight forwarding website.
I'd like the default taxonomy value to be "Order Received" when a new Shipment record is created.
How can I do that?
Doesn't seem to be anything in the TYPES plugin that allows for setting a default.
(feature request?)
You can do that with a little code that triggers off the save_post hook (or save_post_{custom-post-slug} hook), like so:
/*
* Set default taxonomy term on custom post
*/
function tssupp_default_term( $post_id, $post, $update ){
// don't mess with existing posts
if ( !$update ) {
$default_term = '23'; // ID of the required term
$taxonomy = "shipment-status"; // slug of the taxonomy
wp_set_post_terms( $post_id, $default_term, $taxonomy );
}
}
add_action( 'save_post_shipment', 'tssupp_default_term', 101, 3 );
That should work whether you create posts in the front end or back end.
Note that the default is only applied when the post is saved, not when the page to add a new post first loads.
I've submitted a feature request to be able to specify a default in the settings.