Make WordPress Core


Ignore:
Timestamp:
07/14/2020 04:39:44 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Taxonomy: Make some adjustments to handling default terms for custom taxonomies:

  • Move default term assignment from wp_set_object_terms() to wp_insert_post().
  • Make sure the passed taxonomy list overwrites the existing list if not empty.
  • Remove the default term option on unregister_taxonomy().
  • Prevent deletion of the default term in wp_delete_term().

Props enrico.sorcinelli, TimothyBlynJacobs.
See #43517.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r48462 r48480  
    40374037    if ( 'auto-draft' !== $post_status ) {
    40384038        foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) {
    4039             if ( ! empty( $tax_object->default_term ) && ( empty( $postarr['tax_input'] ) || ! isset( $postarr['tax_input'][ $taxonomy ] ) ) ) {
    4040                 $postarr['tax_input'][ $taxonomy ] = array();
     4039
     4040            if ( ! empty( $tax_object->default_term ) ) {
     4041
     4042                // Filter out empty terms.
     4043                if ( isset( $postarr['tax_input'] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) {
     4044                    $postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] );
     4045                }
     4046
     4047                // Passed custom taxonomy list overwrites existing list if not empty.
     4048                $terms = wp_get_object_terms( $post_ID, $taxonomy, array( 'fields' => 'ids' ) );
     4049                if ( ! empty( $terms ) && empty( $postarr['tax_input'][ $taxonomy ] ) ) {
     4050                    $postarr['tax_input'][ $taxonomy ] = $terms;
     4051                }
     4052
     4053                if ( empty( $postarr['tax_input'][ $taxonomy ] ) ) {
     4054                    $default_term_id = get_option( 'default_taxonomy_' . $taxonomy );
     4055                    if ( ! empty( $default_term_id ) ) {
     4056                        $postarr['tax_input'][ $taxonomy ] = array( (int) $default_term_id );
     4057                    }
     4058                }
    40414059            }
    40424060        }
Note: See TracChangeset for help on using the changeset viewer.