Skip Navigation

[Resolved] How can I set a DEFAULT taxonomy field value for custom post types?

This support ticket is created 6 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#957081

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?)

#957195

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Paul

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.