Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#51292 closed defect (bug) (fixed)

`wp_publish_post` does not add default category to auto-drafts

Reported by: peterwilsoncc's profile peterwilsoncc Owned by: peterwilsoncc's profile peterwilsoncc
Milestone: 5.6 Priority: normal
Severity: normal Version: 2.1
Component: Posts, Post Types Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

It's possible to publish a post without any categories by creating an auto-draft and then calling wp_publish_post.

It looks like this can only happen by transitioning from an auto-draft via code. To reproduce:

<?php
$post = wp_insert_post( /* status: auto-draft */ );
wp_publish_post( $post );

Expected behaviour: wp_publish_post() should add the default category if no categories are set on the post.

Attachments (3)

51292.diff (12.7 KB) - added by peterwilsoncc 3 years ago.
51292.2.diff (10.1 KB) - added by peterwilsoncc 3 years ago.
51292.3.diff (11.0 KB) - added by peterwilsoncc 3 years ago.

Download all attachments as: .zip

Change History (11)

This ticket was mentioned in PR #527 on WordPress/wordpress-develop by peterwilsoncc.


3 years ago
#1

  • Keywords has-patch has-unit-tests added

Trac ticket: https://core.trac.wordpress.org/ticket/51292

This also includes the unit tests that will be required for scaling term counts but they probably should have existed all along and I couldn't find any that did.

#2 @peterwilsoncc
3 years ago

  • Milestone changed from Awaiting Review to 5.6

Moving the the 5.6 milestone as this will be required for #40351.

@peterwilsoncc
3 years ago

TimothyBJacobs commented on PR #527:


3 years ago
#3

Does this need to account for the new default_term argument for custom taxonomies or are they not affected?

peterwilsoncc commented on PR #527:


3 years ago
#4

@TimothyBJacobs Nice catch, they are also affected. Further changes to come.

peterwilsoncc commented on PR #527:


3 years ago
#5

@TimothyBJacobs It does now, see 7c9a69b -- any further thought would be welcome.

#6 @peterwilsoncc
3 years ago

  • Owner set to peterwilsoncc
  • Status changed from new to assigned

In 51292.2.diff:

  • Adds default terms for taxonomies if required in wp_publish_post()
  • Unit tests to ensure default terms are added
  • Unit tests to ensure default terms do not override existing terms
  • Tests to ensure term counts are modified accordingly. It could be argued that these tests do not belong on this ticket. Writing these tests for another ticket led me to discover this bug so that's why they are included.

Props due to @TimothyBlynJacobs for review on pull request.

#7 @peterwilsoncc
3 years ago

51292.3.diff incorporates final feedback from Timothy Jacobs and Fränk Klein. Committing once I can get confirmation on how to correctly props that latter.

#8 @peterwilsoncc
3 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 49000:

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.

Note: See TracTickets for help on using tickets.