Make WordPress Core

Opened 15 years ago

Closed 4 years ago

Last modified 4 years ago

#19954 closed defect (bug) (fixed)

wp_insert_post overwrites categories when updating existing post

Reported by: markoheijnen Owned by: johnbillion
Priority: normal Milestone: 6.1
Component: Posts, Post Types Version:
Severity: normal Keywords: has-patch has-unit-tests commit
Cc: Focuses:

Description

When creating an post object with get_default_post_to_edit(), wp_insert_post() will ignored the set category.
It doesn't look if the post has already a good category.

Attachments (2)

19954.patch​ (1.0 KB ) - added by markoheijnen 15 years ago.
Does fix the issue. Maybe need better formating
19954.diff​ (3.7 KB ) - added by leewillis77 10 years ago.
Patch

Download all attachments as: .zip

Change History (18)

@markoheijnen
15 years ago

Does fix the issue. Maybe need better formating

#1 @nacin
15 years ago

  • Milestone Awaiting Review → 3.4

#2 @hughwillfayle
15 years ago

  • Cc t.herzog@… added

Why should this be? WordPress sets category to the default one if there hasn't been setted one.

#3 @jkudish
15 years ago

  • Keywords has-patch added

#4 @nacin
15 years ago

Could we have some example code that triggers this?

Is this an issue to fix/benefit XML-RPC changes in 3.4 at this point?

#5 @markoheijnen
15 years ago

I don't believe it is xml-rpc related. What I did was adding the category with the metod wp_set_object_terms() on the post ID what you get from get_default_post_to_edit(). I know thisis a rare condition.

#6 @ryan
14 years ago

  • Milestone 3.4 → Future Release

#7 @markoheijnen
14 years ago

#22220 was marked as a duplicate.

#8 @chriscct7
11 years ago

  • Keywords needs-refresh added

@leewillis77
10 years ago

Patch

#9 @leewillis77
10 years ago

  • Keywords has-unit-tests added; needs-refresh removed

Hi;

I just hit this bug in a project. The issue is that if you call wp_insert_post() with an existing post ID, and do not supply post_category then any categories assigned to the post will be removed and replaced with the default category.

Patch with unit tests attached.

#10 @leewillis77
10 years ago

  • Summary wp_insert_post should look if default category is nessesary → wp_insert_post overwrites categories when updating existing post

#11 @joehoyle
10 years ago

  • Owner set to joehoyle
  • Status new → reviewing

This ticket was mentioned in ​PR #3057 on ​WordPress/wordpress-develop by ​desrosj.


4 years ago
#12

Trac ticket:

#13 @desrosj
4 years ago

  • Keywords needs-testing added
  • Milestone → 6.1
  • Owner joehoyle removed

Looks like this still exists today.

Since it has not had any activity in 6+ years, I'm resetting the Owned by field. If anyone that has participated in the past would like to reclaim it, please feel free.

I've refreshed 19954.diff​ to apply to the current trunk and adjusted the approach slightly. The coverage of this area in wp_insert_post() is ​actually quite good. With the added tests, I think this is a pretty safe change to make, but would still like some additional testing and sanity checking.

Version 0, edited 4 years ago by desrosj (next)

#14 @johnbillion
4 years ago

  • Keywords commit added; needs-testing removed

#15 @johnbillion
4 years ago

  • Owner set to johnbillion
  • Resolution → fixed
  • Status reviewing → closed

In 53883:

Posts, Post Types: Prevent categories from being overwritten when updating a post using wp_insert_post().

This prevents existing category relationships being overridden with the default category when none is provided in the post data.

Props markoheijnen, leewillis77, desrosj

Fixes #19954

Note: See TracTickets for help on using tickets.