Make WordPress Core

Changeset 49000


Ignore:
Timestamp:
09/19/2020 01:20:48 AM (5 years ago)
Author:
peterwilsoncc
Message:

Posts, Post Types: Ensure default terms are added by wp_publish_post().

Transitioning posts from auto-draft to publish via wp_publish_post() could result in published posts without the default category or custom taxonomy default terms.

Props frank-klein, TimothyBlynJacobs, peterwilsoncc.
Fixes #51292.

Location:
trunk
Files:
2 added
1 edited

Legend:

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

    r48976 r49000  
    43764376    }
    43774377
     4378    // Ensure at least one term is applied for taxonomies with a default term.
     4379    foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) {
     4380        // Skip taxonomy if no default term is set.
     4381        if (
     4382            'category' !== $taxonomy &&
     4383            empty( $tax_object->default_term )
     4384        ) {
     4385            continue;
     4386        }
     4387
     4388        // Do not modify previously set terms.
     4389        if ( ! empty( get_the_terms( $post, $taxonomy ) ) ) {
     4390            continue;
     4391        }
     4392
     4393        if ( 'category' === $taxonomy ) {
     4394            $default_term_id = (int) get_option( 'default_category', 0 );
     4395        } else {
     4396            $default_term_id = (int) get_option( 'default_term_' . $taxonomy, 0 );
     4397        }
     4398
     4399        if ( ! $default_term_id ) {
     4400            continue;
     4401        }
     4402        wp_set_post_terms( $post->ID, array( $default_term_id ), $taxonomy );
     4403    }
     4404
    43784405    $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
    43794406
Note: See TracChangeset for help on using the changeset viewer.