mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-06 13:44:30 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@49000 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -4375,6 +4375,33 @@ function wp_publish_post( $post ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure at least one term is applied for taxonomies with a default term.
|
||||
foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) {
|
||||
// Skip taxonomy if no default term is set.
|
||||
if (
|
||||
'category' !== $taxonomy &&
|
||||
empty( $tax_object->default_term )
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Do not modify previously set terms.
|
||||
if ( ! empty( get_the_terms( $post, $taxonomy ) ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( 'category' === $taxonomy ) {
|
||||
$default_term_id = (int) get_option( 'default_category', 0 );
|
||||
} else {
|
||||
$default_term_id = (int) get_option( 'default_term_' . $taxonomy, 0 );
|
||||
}
|
||||
|
||||
if ( ! $default_term_id ) {
|
||||
continue;
|
||||
}
|
||||
wp_set_post_terms( $post->ID, array( $default_term_id ), $taxonomy );
|
||||
}
|
||||
|
||||
$wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
|
||||
|
||||
clean_post_cache( $post->ID );
|
||||
|
||||
Reference in New Issue
Block a user