You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be great to have a feature to automatically associate a Pod with a given category, with the option of either doing it as a default category (no other categories are selected) or as a fixed category (system will add the category each time the Pod is saved, or will resist attempts to remove it). I've been given a code snippet that does this in a crude way - it would be nice to have a cleaner version in the Pod settings.
/*** Author: Michael Fields* Source: http://wordpress.mfields.org/2010/set-default-terms-for-your-custom-taxonomies-in-wordpress-3-0/* Thanks a lot for the nice tweak*//** * Define default terms for custom taxonomies in WordPress 3.0.1 * * @author Michael Fields http://wordpress.mfields.org/ * @props John P. Bloch http://www.johnpbloch.com/ * * @since 2010-09-13 * @alter 2010-09-14 * * @license GPLv2 */functionmfields_set_default_object_terms( $post_id, $post ) {
if ( 'publish' === $post->post_status ) {
$defaults = array(
'post_tag' => array( 'taco', 'banana' ),
'monkey-faces' => array( 'see-no-evil' ),
);
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( (array) $taxonomiesas$taxonomy ) {
$terms = wp_get_post_terms( $post_id, $taxonomy );
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
}
}
}
}
add_action( 'save_post', 'mfields_set_default_object_terms', 100, 2 );
/*** Just change the 'post_tag' with the taxonomy slug you want to target* and change 'taco' and 'banana' with the slug of the term you want to make default* you can add multiple taxonomy at once so the line#19 is applicable only then*/
Type: FeatureFeatures that add entirely new functionality that was not there beforeType: EnhancementEnhancements to features that already exist, but are not major additions
1 participant
Converted from issue
This discussion was converted from issue #4430 on August 11, 2022 14:26.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
It would be great to have a feature to automatically associate a Pod with a given category, with the option of either doing it as a default category (no other categories are selected) or as a fixed category (system will add the category each time the Pod is saved, or will resist attempts to remove it). I've been given a code snippet that does this in a crude way - it would be nice to have a cleaner version in the Pod settings.
Beta Was this translation helpful? Give feedback.
All reactions